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.
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.
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.
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.
Models can be defined in two equivalent ways in Sequelize: Calling sequelize.define(modelName, attributes, options) Extending Model and calling init(attributes, options)
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'
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With