Set defaultValue to todays date in a Sequelize migration
Asked Answered
S

9

36

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.

Shirt answered 19/11, 2016 at 15:37 Comment(0)
A
36

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.

Astronomical answered 19/11, 2016 at 15:53 Comment(2)
Didn't work for me (2022-03-09), had to use Sequelize.fn('now')Erasmoerasmus
Doesn't work for me on v6.33.0 (2023-10-25), still have to use Sequelize.fn('now')Footrest
B
32

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

Bernardinabernardine answered 7/5, 2019 at 3:15 Comment(2)
I need to use Sequelize.fn('now') on v5 also.Griqua
I had to use Sequelize.fn('NOW') ("now" in uppercase)Bassist
C
20

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

  createdAt: {
                field: 'created_at',
                type: DataTypes.DATE,
                allowNull: false,
                defaultValue: DataTypes.NOW
            }
Cloutman answered 25/2, 2020 at 14:47 Comment(0)
H
6

SQL Server users must use literal('CURRENT_TIMESTAMP')

createdAt: {
   field: 'created_at',
   type: DataTypes.DATE,
   allowNull: false,
   defaultValue: literal('CURRENT_TIMESTAMP'),
}
Huggermugger answered 25/5, 2021 at 16:14 Comment(2)
ERROR: literal is not defined: this does not work for me.Iden
So sorry I just address this. For whoever has the same issue, literal must be imported from sequelize, it is a helper function.Huggermugger
I
4

Use "literal", see below.

updated_at: {
  allowNull: false,
  type: Sequelize.DATE, 
  defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
}
Iden answered 24/11, 2021 at 7:52 Comment(0)
F
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)
  }
});
Forworn answered 18/5, 2022 at 4:55 Comment(0)
A
1

I had to use

createdAt: {
    type: DataTypes.DATE,
    defaultValue: Sequelize.fn('getdate')
},
Apostasy answered 19/11, 2020 at 6:34 Comment(0)
D
1

Only this worked for me.

createdAt: {
   field: 'created_at',
   type: DataTypes.DATE,
   allowNull: false,
   defaultValue: Sequelize.fn('NOW')
}
Doggy answered 23/3, 2021 at 12:25 Comment(1)
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 youZn
D
0

Only this worked for me.

sale_date: {
  allowNull: false,
  type: Sequelize.DATE,
  defaultValue: Sequelize.fn('now')
},
Drape answered 3/11, 2022 at 19:12 Comment(1)
Your answer could be improved by adding more information on what the code does and how it helps the OP.Governor

© 2022 - 2024 — McMap. All rights reserved.