【问题标题】:disable updatedAt (update date) field in sequelize.js在 sequelize.js 中禁用 updatedAt(更新日期)字段
【发布时间】:2021-03-22 12:43:13
【问题描述】:

我使用 sequelize-auto 生成架构,我尝试使用 findOne() 并收到此错误:

Unhandled rejection SequelizeDatabaseError: Invalid column name 'updatedAt'.

在我的数据库表中没有字段updatedAt

例如我的表名是Users我的代码是Users.findOne(),而users表中没有updatedAt字段。

db.users= sequelize.import(__dirname + "/models/users");
app.get('/users', function (req, res) {

  db.user.findOne().then(function (project) {
    res.json(project);
  })

});

如何解决?

【问题讨论】:

    标签: node.js sequelize.js tedious


    【解决方案1】:

    Sequelize中Model的配置参考documentation

    在你的表的模型对象中应该是这样的。

    var user = sequelize.define('user', { /* bla */ }, {
    
      // don't add the timestamp attributes (updatedAt, createdAt)
      timestamps: false,
    
      // If don't want createdAt
      createdAt: false,
    
      // If don't want updatedAt
      updatedAt: false,
    
      // your other configuration here
    
    });
    

    检查Usage of sequelize-auto-generation模块

    1. 为所有模型配置选项创建一个 json 文件(标志对象定义为here)。

    2. 在执行命令时,您必须传递 -a--addtional 选项,以便传递 json 文件配置路径。

    【讨论】:

    • 哦,我也明白了,所以我们必须在 sequelize-auto generation 中添加配置选项,你能帮我如何添加配置吗?我只是尝试但仍然失败
    • 我在回答中添加了一些说明
    • 问题似乎是关于只禁用 updatedAt,而不是两列。我相信这不应该是公认的答案。
    • 这种做法应该是正确答案:stackoverflow.com/questions/45248189/…
    • @RafaelCalhau 感谢您的关注。我已根据您的关注更改了答案。
    猜你喜欢
    • 1970-01-01
    • 2021-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-20
    相关资源
    最近更新 更多