Adding CURRENT_TIMESTAMP as a "DEFAULT" and "ON UPDATE" for createdAt, updatedAt on migrations
See original GitHub issueI am using migrations to create and update my MySQL DB schemas. I am using underscored versions of createdAt, updatedAt. Here is what I have in my migration file:
created_at: {
allowNull: false,
type: Sequelize.DATE
},
updated_at: {
allowNull: false,
type: Sequelize.DATE
}
When I run the migration, I would expect “DEFAULT” to be set to CURRENT_TIMESTAMP for “created_at” and “ON UPDATE” to be set to CURRENT_TIMESTAMP for “updated_at”, but it doesn’t happen.
So, I am trying to set them myself. I have tried adding:
defaultValue: Sequelize.NOW updatedAt: Sequelize.NOW defaultValue: Sequelize.fn(‘NOW’) updatedAt: Sequelize.fn(‘NOW’) defaultValue: ‘CURRENT_TIMESTAMP’ updatedAt: ‘CURRENT_TIMESTAMP’ default: Sequelize.NOW
…all to no avail. I can’t find any way to get “DEFAULT CURRENT_TIMESTAMP” or “ON UPDATE CURRENT_TIMESTAMP” to run in the query.
I know that sequelize handles the create and update times automatically, but it would be nice to be able to also put these in the raw SQL to allow SQL to handle them (in the case of manually making edits to the database, etc.)
Thanks!
Issue Analytics
- State:
- Created 8 years ago
- Comments:26 (3 by maintainers)
Top Related StackOverflow Question
I tried following:
and I got this table:
My env is
Sequelize [Node: 4.3.2, CLI: 2.4.0, ORM: 3.27.0, mysql: ^2.12.0]Although it is closed 😃
defaultValue: sequelize.literal('CURRENT_TIMESTAMP)might work, not sure though.