在更新Current_timestamp上使用的语法'

发布于 2025-02-06 04:32:39 字数 1961 浏览 3 评论 0原文

我想使用时间戳列createat - &gt; 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪漫之都 2025-02-13 04:32:39

最近,我在使用续集时遇到了相同的语法错误,事实证明,对于那些刚从SQL延新或转移的人来说,遇到此错误是很常见的。可以通过确保正确的语法来解决错误。

而不是像本机SQL值 current_timestamp上更新Current_timestamp 用于更新 timestamp :: ::

export const User = sequelize.define(
  '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 }
);

尝试: onupdate在sequelize中的选项,该选项在连续更新值时会更新timestamp

export const User = sequelize.define(
  'user',
  {
    .....
    createdAt: {
      type: 'TIMESTAMP',
      allowNull: false,
      defaultValue: sequelize.literal('CURRENT_TIMESTAMP'),
      field: 'created_at',
    },
    updatedAt: {
      type: 'TIMESTAMP',
      allowNull: false,
      defaultValue: Sequelize.literal("CURRENT_TIMESTAMP"),
      onUpdate: Sequelize.literal("CURRENT_TIMESTAMP"),
      field: 'updated_at',
    },
  },
  { timestamps: false }
);

希望这会有所帮助。

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 the TIMESTAMP :

export const User = sequelize.define(
  '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 }
);

Try the: onUpdate option in sequelize which updates the TIMESTAMP upon updating values in a row.

export const User = sequelize.define(
  'user',
  {
    .....
    createdAt: {
      type: 'TIMESTAMP',
      allowNull: false,
      defaultValue: sequelize.literal('CURRENT_TIMESTAMP'),
      field: 'created_at',
    },
    updatedAt: {
      type: 'TIMESTAMP',
      allowNull: false,
      defaultValue: Sequelize.literal("CURRENT_TIMESTAMP"),
      onUpdate: Sequelize.literal("CURRENT_TIMESTAMP"),
      field: 'updated_at',
    },
  },
  { timestamps: false }
);

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文