在Sequelize中仅启用createdAt时间戳并忽略updateAt时间戳

时间:2019-05-29 05:12:16

标签: mysql node.js express sequelize.js

我试图在表中创建一些记录,只填充了createdAt列。我希望在创建模型时,updateAt列不存在,它会自动生成createdAt和UpdatedAt时间戳。

我尝试使用timestamps : false,但createdAt列包含空值。

const MyTable = sequelize.define(
  'my_table',
  {
    id: {
      type: DataTypes.INTEGER(11),allowNull: false,
      primaryKey: true,autoIncrement: true,
    },
    person_name: {type: DataTypes.STRING(255),allowNull: false},
    created: {type: DataTypes.DATE,allowNull: true},
  },
  {
    tableName: 'my_table',
    timestamps: false,
    createdAt: 'created',
  },
);

是否可以在模型本身中解决此问题而无需在查询中进行任何更改?

1 个答案:

答案 0 :(得分:1)

根据the documentation on Init,您想要保持时间戳记功能为打开状态,但是将模型配置为不使用updatedAt

这是示例代码中options对象的编辑版本,应该可以为您提供所需的内容:

  {
    tableName: 'my_table',
    updatedAt: false,
  }

注意:文档说“时间戳记必须为真”,这可能表明为什么它不能按照您提供的示例中的方式工作。

干杯!