-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Defining a default ON UPDATE behavior for models, such as ON UPDATE CURRENT_TIMESTAMP #5561
Comments
I created a solution which is run after Now other apps which use the same database without using Sequelize will benefit from these defaults. I do not know if this kind of change would be accepted with the Sequelize code base. I found a couple of related issues in the past couple of years. Queries
Alter Table
|
If |
This would need to be changed in the way that a model is defined. One suggestion is var user = sequelize.define('user', { /* attributes */ }, {
timestamps: true,
createdAt: {
field: 'beginTime',
defaultValue: sequelize.literal('NOW()'),
},
}); but I believe that's a change that will break current functionality with insert/update queries (haven't run any tests yet). @mickhansen Is there another preferred way to add the option of a |
Adding createdAt as a field with a literal default value should work just fine var user = sequelize.define('user', {
createdAt: {
type: DataTypes.DATE,
field: 'beginTime',
defaultValue: sequelize.literal('NOW()')
}
}, {
timestamps: true,
}); |
hi guys, if you need to set update on module.exports = function (sequelize, DataTypes) {
const Model = sequelize.define('TUser', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
unique: true,
field: 'id',
},
'created_at': {
type: DataTypes.DATE(3),
defaultValue: sequelize.literal('CURRENT_TIMESTAMP(3)'),
},
'updated_at': {
type: DataTypes.DATE(3),
defaultValue: sequelize.literal('CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)'),
},
}, {
timestamps: true,
tableName: 't_user',
paranoid: true,
underscored: true,
});
return Model
}; |
@Here21 It inserts |
@mysuf What's your MySQL version? |
@Here21 mysql-server 5.7.24 |
@mysuf if above you had checked and no problem, I guess the problem happens at the Sequelize version. |
However, I believe the defaults could be changed to this, so maybe this issue needs to be re-opened. Is the difficulty there to track the exact sql invocation for each database? Edit: hmm, spoke too soon, it works when syncing, it fails as it did for @mysuf above at #5561 (comment) The problem seems to be the following: Sequelize doesn't distinguish bt the defaultValue that should be provided when doing the table syncing vs the value that should be sent to db during inserts. Should we reopen this issue? |
Meanwhile it is possible to solve this with the following: createdAt: {
type: 'DATETIME(3) DEFAULT CURRENT_TIMESTAMP(3)'
},
updatedAt: {
type: 'DATETIME(3) DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)'
} I don't know if it's the correct way by the way. |
this worked for me
|
I tried multiple solutions that were suggested in this thread and also in #4896, but what @miladabc suggested was the only thing 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). Also wanted to add that if you are using createdAt: {
type: 'DATETIME(3) DEFAULT CURRENT_TIMESTAMP(3) NOT NULL',
field: 'created_at',
defaultValue: () => new Date(),
},
updatedAt: {
type: 'DATETIME(3) DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3) NOT NULL',
field: 'updated_at',
defaultValue: () => new Date(),
}, This way Sequelize will not inject it in |
sequelize/sequelize#5561 - there is issue with timestamps and I found only this solution to work for me
I want tables created with the definitions below but I do not see how I can do that with Sequelize when these columns are automatically defined. This is also being done with a MySQL database.
The reason I want it defined in the database is because multiple apps use this database and not all are built with Sequelize. And I want to enforce these default values. Is there a way to have Sequelize create tables with the schema shown below? Or would an update after the tables are created be necessary to older these columns?
I am looking through the docs and I do not see how these columns would be created as I intend.
The text was updated successfully, but these errors were encountered: