node.js 避免由 sequelize 自动生成 created_at 和 updated_at

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36906500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:09:42  来源:igfitidea点击:

Avoid created_at and updated_at being auto generated by sequelize

node.jspostgresqlsequelize.js

提问by Tyler Brock

How do I define a model for which created_at and updated_at are provided rather than generated?

如何定义提供而不是生成 created_at 和 updated_at 的模型?

I'm importing data from somewhere that already has data for created_atand updated_atfields that I would like to preserve rather than generating whenever the object is created/updated by sequelize (our secondary store).

我正在从已经有数据的地方导入数据created_atupdated_at我想保留的字段,而不是在 sequelize(我们的二级存储)创建/更新对象时生成。

I've tried every likely permutation of model definition and options to get this to work and still sequelize overwrites my fields with it's own timestamps: {silent: true}, etc...

我已经尝试了模型定义和选项的所有可能排列来使其工作,并且仍然使用自己的时间戳覆盖我的字段:{silent: true}等等......

To be clear, the input data has createdAt and updatedAt and I'd like to use sequelize's bulkCreate(), etc such that input values provided are used and stored on the model as created_at and updated_at rather than generated by sequelize.

需要明确的是,输入数据有createdAt 和updatedAt,我想使用sequelize 的bulkCreate() 等,以便使用提供的输入值并将其作为created_at 和updated_at 存储在模型中,而不是由sequelize 生成。

This is my current model definition:

这是我当前的模型定义:

const Lead = sequelize.define('lead', {
  objectId: {
    type: Sequelize.STRING,
    field: 'objectId',
    primaryKey: true,
    allowNull: false
  },
  firstName: {
    type: Sequelize.STRING,
    field: 'first_name'
  },
  lastName: {
    type: Sequelize.STRING,
    field: 'last_name'
  },
  phoneNumber: {
    type: Sequelize.STRING,
    field: 'phone_number'
  },
  createdAt: {
    type: Sequelize.DATE,
    field: 'created_at',
  },
  updatedAt: {
    type: Sequelize.DATE,
    field: 'updated_at'
  }
}, {
  freezeTableName: true, // Model tableName will be the same as the model name
  timestamps: false,
  underscored: true
});

回答by Tudor Constantin

The option is

选项是

timestamps: false,

Where timestampsis written fully lowercase

哪里timestamps全小写

Update:

更新:

From looking at the code of the bulkCreate()function, it looks that it always updates the timestamps, so, without a patch this is not possible right now

从查看bulkCreate()函数的代码来看,它看起来总是更新时间戳,因此,如果没有补丁,现在是不可能的