-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding CURRENT_TIMESTAMP as a "DEFAULT" and "ON UPDATE" for createdAt, updatedAt on migrations #4896
Comments
|
That worked beautifully! Thanks! Still trying to figure out the "ON UPDATE CURRENT_TIMESTAMP" case, if you have any ideas. |
Does that work with default value or is it an extra field attribute? |
Hmmm, then that is likely not possible with native sequelize at the moment. |
OK, thanks! |
I tried following:
and I got this table:
My env is Although it is closed :) |
@shun-tak Thanks man! module.exports = function (sequelize, DataTypes) {
const Model = sequelize.define('TUser', {
createdAt: {
type: DataTypes.DATE(3),
defaultValue: sequelize.literal('CURRENT_TIMESTAMP(3)'),
field: 'created_at',
},
updatedAt: {
type: DataTypes.DATE(3),
defaultValue: sequelize.literal('CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)'),
field: 'updated_at',
},
}, {
tableName: 't_user',
paranoid: true,
});
return Model
}; |
But how it will act while insertion. Sequelize orm showing error |
SequelizeDatabaseError: 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 'ON UPDATE CURRENT_TIMESTAMP,1,1) |
@kartik115 Just insert like this:
or
Please see the MySQL manual. |
@shun-tak I got this error when I was using sequelize orm to create a new user. So it showing me syntax error. I have used |
Same error. Message:
I've read about Sequelize operators on querying, but there's nothing abou 'ON'. :( |
@kartik115 using in sequelize version 3.21.0, |
Anyone coming here in 2018 @shun-tak method no longer works for
Basically remove the 'ON UPDATE CURRENT_TIMESTAMP(3)' otherwise this will result in SQL syntax error when you try to update. Like such:
|
@amwill04, Does your solution updates the updatedAt attribute automatically? Or that should be model logic? |
In version 4.38.0, the file sequelize/lib/dialects/mysql/query-generator.js uses |
It works for me queryInterface.createTable('User', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
name: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP')
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
}
}); |
@cloudge please, tell about you sequelize version. |
With => "sequelize": "^4.38.0", On Model :
& On queryInterface for migration !
|
All variants doesn't work correctly for me in postgres. They are INVOKED (WHY?!) and timestamp is setted to time of model's script execution. Not literal, not simple string 'now', nothing else doesn't work. |
Only solution which actually works when updated with both code and DB directly. Kudos. |
this solved my problem. thank you so much! here's what i did earlier: created_on: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: "CURRENT_TIMESTAMP(6)"
} it was solved with: created_on: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: sequelize.literal("CURRENT_TIMESTAMP(6)")
} |
For those who are still confused, consider the code below: First, use const sample = (sequelize, Sequelize) =>
sequelize.define("sample", {
createdAt: {
type: Sequelize.DATE,
defaultValue: Sequelize.literal("CURRENT_TIMESTAMP"),
},
updatedAt: {
type: Sequelize.DATE,
defaultValue: Sequelize.literal(
"CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"
),
},
}); Then, use const Sample = sample(sequelize, Sequelize);
Sample.create({ updatedAt: Sequelize.literal("CURRENT_TIMESTAMP") }).then(
(result) => {
// do whatever
}
); |
works for now.
|
ERROR: TIMESTAMP is not defined "dependencies": { |
Just a quick note who also spent a lot of time as me to make updatedAt: {
type: Sequelize.DATE,
defaultValue: Sequelize.literal("CURRENT_TIMESTAMP"),
} |
what version are you using? |
The suggestion by @miladabc in #5561 was the only one that worked both in the app and the DB (i.e. no matter where the entry was inserted/updated, the right timestamps were always set). See #5561 (comment) for more details |
I 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:
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!
The text was updated successfully, but these errors were encountered: