31

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.

Andreas
  • 2,287
  • 2
  • 23
  • 26

9 Answers9

32

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.

drinchev
  • 19,201
  • 4
  • 67
  • 93
30

Sequelize.NOW will work in future but some versions(I tried 4.43.0) seem not support it. I succeeded in setting now() with using Sequelize.fn('now') .

{
  type: Sequelize.DATEONLY,
  allowNull: false,
  defaultValue: Sequelize.fn('now')
}

Reference: https://github.com/sequelize/sequelize/issues/4679

asukiaaa
  • 1,594
  • 17
  • 19
18

https://github.com/sequelize/sequelize/issues/645#issuecomment-18461231

  createdAt: {
                field: 'created_at',
                type: DataTypes.DATE,
                allowNull: false,
                defaultValue: DataTypes.NOW
            }
Sharan
  • 1,055
  • 3
  • 21
  • 38
4

SQL Server users must use literal('CURRENT_TIMESTAMP')

createdAt: {
   field: 'created_at',
   type: DataTypes.DATE,
   allowNull: false,
   defaultValue: literal('CURRENT_TIMESTAMP'),
}
Yuyo
  • 41
  • 3
3

Use "literal", see below.

updated_at: {
  allowNull: false,
  type: Sequelize.DATE, 
  defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
}
Donald Kagunila
  • 329
  • 2
  • 5
2

TIME: 2022-05-18

According to latest Sequelize Doc (version v6)

sequelize.define('Foo', {
  bar: {
     type: DataTypes.DATETIME,
     defaultValue: DataTypes.NOW
     // This way, the current date/time will be used to populate this column 
     (at the moment of insertion)
  }
});
Steven Yeh
  • 21
  • 2
1

I had to use

createdAt: {
    type: DataTypes.DATE,
    defaultValue: Sequelize.fn('getdate')
},
Dilhan Maduranga
  • 2,201
  • 1
  • 16
  • 12
1

Only this worked for me.

createdAt: {
   field: 'created_at',
   type: DataTypes.DATE,
   allowNull: false,
   defaultValue: Sequelize.fn('NOW')
}
jiminikiz
  • 2,867
  • 1
  • 25
  • 28
  • could you put the json into a code block so that it is easier to read and explain a little about why this worked for you – Cookie Mar 23 '21 at 17:01
0

Only this worked for me.

sale_date: {
  allowNull: false,
  type: Sequelize.DATE,
  defaultValue: Sequelize.fn('now')
},
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
  • 1
    Your answer could be improved by adding more information on what the code does and how it helps the OP. – Tyler2P Nov 07 '22 at 15:39