【问题标题】:sequelize - update row without altering the updated_at fieldsequelize - 在不改变 updated_at 字段的情况下更新行
【发布时间】:2016-07-02 01:46:57
【问题描述】:

我在节点开发中使用 sequelize.js 库作为 ORM。当使用 sequelize 更新一行时,'updated_at' 字段更改为当前时间戳。 我想知道如何防止这种情况发生? 在不更改 'updated_at' 字段的情况下,我想使用 sequlize API 更新其他字段,而不是运行原始查询。

【问题讨论】:

    标签: node.js orm sequelize.js


    【解决方案1】:

    在您的模型定义中,您可以turn timestamps off 指定特定字段。

    如果您希望 sequelize 处理时间戳,但只需要其中一些,或者希望您的时间戳被称为其他名称,您可以单独覆盖每一列:

    var Foo = sequelize.define('foo',  { /* bla */ }, {
      // don't forget to enable timestamps!
      timestamps: true,
    
      // I don't want createdAt
      createdAt: false,
    
      // I want updatedAt to actually be called updateTimestamp
      updatedAt: 'updateTimestamp'
    
      // And deletedAt to be called destroyTime (remember to enable paranoid for this to work)
      deletedAt: 'destroyTime',
      paranoid: true
    });
    

    否则,您可以关闭时间戳并根据需要在每次更新时手动处理它们。

    【讨论】:

      【解决方案2】:

      根据http://docs.sequelizejs.com/class/lib/model.js~Model.html#static-method-update,您可以使用“静默”选项运行更新查询,但不更新 updateAt 字段。

      options.silent 布尔值
      可选
      默认值:假
      如果为 true,则不会更新 updatedAt 时间戳。

      myModelInstance.update(values, {
          silent: true
      })
      

      【讨论】:

      • 这应该是您不需要更改此解决方案架构的公认答案
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-15
      • 1970-01-01
      • 2014-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      相关资源
      最近更新 更多