在更新Current_timestamp上使用的语法'
我想使用时间戳列createat
- > create_at
,<代码>更新 - &gt; Updated_at
当我更新任何行时,我想将列update_at更改为current_timestamp
。
因此,我这样写了,
export const User = sequelize.define(
'user',
{
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
name: { type: DataTypes.STRING(15), allowNull: false },
nickname: { type: DataTypes.STRING(15), allowNull: false },
email: { type: DataTypes.STRING(15), allowNull: false, unique: true },
password: { type: DataTypes.STRING(15), allowNull: false },
role: {
type: DataTypes.STRING(30),
allowNull: false,
defaultValue: 'USER',
},
createdAt: {
type: 'TIMESTAMP',
allowNull: false,
defaultValue: sequelize.literal('CURRENT_TIMESTAMP'),
field: 'created_at',
},
updatedAt: {
type: 'TIMESTAMP',
allowNull: false,
defaultValue: sequelize.literal(
'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'
),
field: 'updated_at',
},
},
{ timestamps: false }
);
每个人都说“ current_timestamp on Update current_timestamp
”它起作用。
当我更新任何列时,是的,它有效。 但是,当我创建任何行时,我可以看到这样的错误。
sqlMessage:“您在SQL语法中有一个错误;检查与您的MySQL Server版本相对应的手册,以便在“更新Current_timestamp”上使用的正确语法'on Line 1 on line 1 on of line code of in line 1”,
?
现在,我使用[email&nbsp; procectioned]
。
那就是desc用户;
表。 在此处输入图像描述
I want to use timestamp column createdAt
-> created_at
, updatedAt
-> updated_at
and when I update any row, I wanna change column update_at to CURRENT_TIMESTAMP
.
So I wrote like this,
export const User = sequelize.define(
'user',
{
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
name: { type: DataTypes.STRING(15), allowNull: false },
nickname: { type: DataTypes.STRING(15), allowNull: false },
email: { type: DataTypes.STRING(15), allowNull: false, unique: true },
password: { type: DataTypes.STRING(15), allowNull: false },
role: {
type: DataTypes.STRING(30),
allowNull: false,
defaultValue: 'USER',
},
createdAt: {
type: 'TIMESTAMP',
allowNull: false,
defaultValue: sequelize.literal('CURRENT_TIMESTAMP'),
field: 'created_at',
},
updatedAt: {
type: 'TIMESTAMP',
allowNull: false,
defaultValue: sequelize.literal(
'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'
),
field: 'updated_at',
},
},
{ timestamps: false }
);
Everyone say "CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
" it works.
When I update any column, Yes, It works.
but When I create any row, I can see error like this.
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 'ON UPDATE CURRENT_TIMESTAMP)' at line 1",
What's the matter?
Now, I use [email protected]
and [email protected]
.
And that is DESC users;
table.
enter image description here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最近,我在使用续集时遇到了相同的语法错误,事实证明,对于那些刚从SQL延新或转移的人来说,遇到此错误是很常见的。可以通过确保正确的语法来解决错误。
而不是像本机SQL值
current_timestamp上更新Current_timestamp
用于更新timestamp
:: ::尝试:
onupdate
在sequelize中的选项,该选项在连续更新值时会更新timestamp
。希望这会有所帮助。
I recently encountered the same syntax error while working with Sequelize, and it turns out it is quite common to run into this error for those who are new to Sequelize or shifting from SQL. The error can be resolved by ensuring the correct syntax.
Instead of writing like a native SQL value
CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
for updating theTIMESTAMP
:Try the:
onUpdate
option in sequelize which updates theTIMESTAMP
upon updating values in a row.Hope this helps.