Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename node.js sequelize timestamp columns

I've just started using sequelize but I'm having a small issue mapping an existing database.

By default sequelize creates two datatime columns named createdAt and updatedAt, does anyone know if its possible to rename the columns to something else. For example...

products: sequelize.define('products', {
    timestamps: false,
    product_id: {
        type: Sequelize.INTEGER, 
        primaryKey: true, 
        autoIncrement: true
    },
    product_name: Sequelize.STRING,
    product_description: Sequelize.TEXT,
    product_created: Sequelize.DATE,
    product_updated: Sequelize.DATE
}),

That would still automagically amend the product_created/product_updated columns on creates and updates.

like image 233
Kinetic Avatar asked Feb 01 '13 19:02

Kinetic


People also ask

How do I rename a column in Sequelize?

You have to drop all the constraints, rename the column and then add the constraints back. With a single constraint on totoId it would look something like this: // 1) drop constraint queryInterface. removeConstraint('my_some_table', 'my_constraint'); // 2) rename column queryInterface.

What is timestamp in Sequelize?

The timestamps option in Sequelize models allows you to add two time-related attributes to the model. The two attributes added to your model are createdAt and updatedAt attributes.

What is Sequelize sync()?

The Sequelize instance method sync() is used to synchronize your Sequelize model with your database tables. The synchronization happens at the table level. When your table doesn't exist the sync() method will generate and run a CREATE TABLE statement for you.

How to define model in Sequelize?

Models can be defined in two equivalent ways in Sequelize: Calling sequelize.define(modelName, attributes, options) Extending Model and calling init(attributes, options)


1 Answers

Another update strikes (and two years pass) and you can do just what you want:

products: sequelize.define('products', {
    timestamps: false,
    product_id: {
        type: Sequelize.INTEGER, 
        primaryKey: true, 
        autoIncrement: true
    },
    product_name: Sequelize.STRING,
    product_description: Sequelize.TEXT,
    product_created: Sequelize.DATE,
    product_updated: Sequelize.DATE
}, {
  updatedAt: 'product_updated',
  createdAt: 'product_created'
});
like image 61
Yan Foto Avatar answered Oct 23 '22 17:10

Yan Foto