0

I need to set the default value of the appliedDate column to the current date.In MySQL there is a function CURRENT_DATE() to get the current date.But I can't seem to find an equivalent with Sequelize. Is it possible in Sequelize? `

appliedDate: {
    type: DataTypes.DATEONLY,
    defaultValue: Sequelize.fn("now"),
  },

` This doesn't work since appliedDate need to be DATEONLY type.

I tried to use Sequelize.fn("now") , Sequelize.literal("CURRENT_DATE") , Sequelize.fn("CURRENT_DATE") but all of these resulted in error. I could use a workaround by setting the date using Javascript, but it would be nice to know if this feature exists in Sequelize.

  • 1
    Did you try `defaultValue: DataTypes.NOW` ? – Pompedup Nov 19 '22 at 08:30
  • Did you use `Sequelize.literal("CURRENT_DATE()") `? What error does Sequelize give you? – Anatoly Nov 19 '22 at 09:12
  • @Anatoly This is what it gives – Maanas O T Nov 19 '22 at 11:23
  • sqlMessage: `"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CURRENT_DATE()' at line 1",` sql: `'ALTER TABLE Jobs CHANGE appliedDate appliedDate DATE DEFAULT CURRENT_DATE();',` – Maanas O T Nov 19 '22 at 11:36
  • What version of MySQL do you have? Look at the answers here https://stackoverflow.com/questions/20461030/current-date-curdate-not-working-as-default-date-value – Anatoly Nov 19 '22 at 13:28
  • @Anatoly Thanks. The thread you sent had the right syntax to use . It is supposed to be `Sequelize.literal("(CURRENT_DATE())")` . MySQL version 8 – Maanas O T Nov 22 '22 at 06:57

1 Answers1

0

defaultValue: Sequelize.literal("(CURRENT_DATE())")

defaultValue: DataTypes.NOW

Both these solve the problem