I'm using Node Express with Sequelize and I want to set the defaultValue for the column "date" in the database to todays date. I tried with the following migration, but it didn't work, as it set the default date to be the same date as when I ran the migration. I want it to be set to the same date as when the row is created.
module.exports = {
up: function (queryInterface, Sequelize) {
return queryInterface.addColumn(
'Todos',
'date',
{
type: Sequelize.DATEONLY,
allowNull: false,
defaultValue: new Date()
}
)
},
I can't understand how that would work.
You need to use as a default value the MySQL function NOW()
.
So your column declaration should look like :
{
type: Sequelize.DATEONLY,
allowNull: false,
defaultValue: Sequelize.NOW
}
Keep in mind that this will not populate the fields in your migration. They will be empty, since they were not created with sequelize.