Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 243
lellefood Avatar asked Jun 07 '18 07:06

lellefood


1 Answers

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.

like image 147
James Bubb Avatar answered Nov 04 '22 15:11

James Bubb