lellefood
lellefood

Reputation: 2086

Add automatic createdAt and updatedAt timestamps in migration

I'm doing a Sequelize migration which adds a new table. In this table I want to add the timestamps createdAt and updatedAt that are automatically updated and my migrations works using literals like this:

createdAt: {
    type: Sequelize.DATE,
    defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
},
updatedAt: {
    type: Sequelize.DATE,
    defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
},

I've to only add timestamps: true in the options but the timestamps are not automatically updated.

Can I do this without using literals?

Upvotes: 11

Views: 12210

Answers (1)

James Bubb
James Bubb

Reputation: 3173

I had a similar problem in setting the defaultValue for the timestamp fields.

I came across this issue: https://github.com/sequelize/sequelize/issues/645 which suggests calling Sequelize.fn('NOW') to automatically update the timestamps.

Seemed to work in my case.

Upvotes: 18

Related Questions