NodeJs报SequelizeDatabaseError Unknown column ‘created_at‘ in ‘field list‘解决方法

Posted 秋9

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NodeJs报SequelizeDatabaseError Unknown column ‘created_at‘ in ‘field list‘解决方法相关的知识,希望对你有一定的参考价值。

现象


    "ret": 0,
    "msg": "获取失败",
    "e": 
        "name": "SequelizeDatabaseError",
        "parent": 
            "code": "ER_BAD_FIELD_ERROR",
            "errno": 1054,
            "sqlState": "42S22",
            "sqlMessage": "Unknown column 'created_at' in 'field list'",
            "sql": "SELECT `id`, `room_id` AS `roomId`, `stu_name` AS `stuName`, `stu_code` AS `stuCode`, `vision_left` AS `visionLeft`, `vision_right` AS `visionRight`, `vision_avg` AS `visionAvg`, `correct_left` AS `correctLeft`, `correct_right` AS `correctRight`, `correct_avg` AS `correctAvg`, `change_yn` AS `changeYn`, `vision_source` AS `visionSource`, `vision_type` AS `visionType`, `created_at` AS `createdAt`, `updated_at` AS `updatedAt` FROM `vision` AS `vision` WHERE `vision`.`room_id` = 1681;"
        ,
        "original": 
            "code": "ER_BAD_FIELD_ERROR",
            "errno": 1054,
            "sqlState": "42S22",
            "sqlMessage": "Unknown column 'created_at' in 'field list'",
            "sql": "SELECT `id`, `room_id` AS `roomId`, `stu_name` AS `stuName`, `stu_code` AS `stuCode`, `vision_left` AS `visionLeft`, `vision_right` AS `visionRight`, `vision_avg` AS `visionAvg`, `correct_left` AS `correctLeft`, `correct_right` AS `correctRight`, `correct_avg` AS `correctAvg`, `change_yn` AS `changeYn`, `vision_source` AS `visionSource`, `vision_type` AS `visionType`, `created_at` AS `createdAt`, `updated_at` AS `updatedAt` FROM `vision` AS `vision` WHERE `vision`.`room_id` = 1681;"
        ,
        "sql": "SELECT `id`, `room_id` AS `roomId`, `stu_name` AS `stuName`, `stu_code` AS `stuCode`, `vision_left` AS `visionLeft`, `vision_right` AS `visionRight`, `vision_avg` AS `visionAvg`, `correct_left` AS `correctLeft`, `correct_right` AS `correctRight`, `correct_avg` AS `correctAvg`, `change_yn` AS `changeYn`, `vision_source` AS `visionSource`, `vision_type` AS `visionType`, `created_at` AS `createdAt`, `updated_at` AS `updatedAt` FROM `vision` AS `vision` WHERE `vision`.`room_id` = 1681;",
        "parameters": 
    

原因:

Nodejs中,在Sequelize创建实例时未设置timestamps参数,timestamps参数含义为是否给每条记录添加createdAt和updatedAt字段,默认为true。通过使此参数为false使其不添加这两个参数即可。

解决方法:

module.exports = app => 
  const  STRING, INTEGER, DATE, BOOLEAN, DOUBLE ,TEXT = app.Sequelize;

  const Vision = app.model.define('vision', 
    id:  type: INTEGER, primaryKey: true, autoIncrement: true ,
    stuName: STRING(32),
    stuCode: STRING(32),
    visionLeft:type: DOUBLE,defaultValue: 0,
    visionRight:type: DOUBLE,defaultValue: 0,
    visionAvg:type: DOUBLE,defaultValue: 0
  ,tableName: 'vision',timestamps: false);
  return Vision;
;

以上是关于NodeJs报SequelizeDatabaseError Unknown column ‘created_at‘ in ‘field list‘解决方法的主要内容,如果未能解决你的问题,请参考以下文章

安装NodeJS环境报错

NodeJS 数据报 - 接收 IPv6 多播

nodejs 插件都已安装好 hbuilder运行报错

nodejs require路径使用变量报错为啥

nodejs npm install 安装屡次报错,求大神帮忙看看啥问题

nodeJs 报maximum call stack size exceeded js