ExpressJS - Sequelize - 列缺失错误

cph*_*ill 3 mysql express sequelize.js

我正在尝试正确查询所有符合我的续集查询的图像以及连接到特定查询的描述,但是我收到一个createdAt列的错误,该列不在我的表中.如何在查询中指定要使用的列?

这是查询(模式和颜色被正确地拉入查询):

router.get('/:pattern/:color/result', function(req, res){

    console.log(req.params.color);
    console.log(req.params.pattern);

    Images.findAll({ 
        where: {
            pattern: req.params.pattern,
            color: req.params.color
        }
        });
        //console.log(image);
        //console.log(doc.descriptions_id);
        res.render('pages/result.hbs', {
            pattern : req.params.pattern,
            color : req.params.color,
            image : image
        });

});
Run Code Online (Sandbox Code Playgroud)

这是我的表:

CREATE TABLE `images` (
  `id` int(5) NOT NULL AUTO_INCREMENT,
  `pattern` varchar(225) DEFAULT NULL,
  `color` varchar(225) DEFAULT NULL,
  `imageUrl` varchar(225) DEFAULT NULL,
  `imageSource` varchar(225) DEFAULT NULL,
  `description_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `description_id` (`description_id`),
  CONSTRAINT `images_ibfk_1` FOREIGN KEY (`description_id`) REFERENCES `description` (`description_id`)
) ENGINE=InnoDB AUTO_INCREMENT=47 DEFAULT CHARSET=latin1;
Run Code Online (Sandbox Code Playgroud)

这是错误:

   Executing (default): SELECT `id`, `pattern`, `color`, `imageUrl`, `imageSource`, `description_id`, `createdAt`, `updatedAt` FROM `images` AS `images` WHERE `images`.`pattern` = 'solid' AND `images`.`color` = 'navy-blue';
    Unhandled rejection SequelizeDatabaseError: ER_BAD_FIELD_ERROR: Unknown column 'createdAt' in 'field list'
        at Query.formatError (/Users/user/Desktop/Projects/node/assistant/node_modules/sequelize/lib/dialects/mysql/query.js:160:14)
Run Code Online (Sandbox Code Playgroud)

Jan*_*ier 9

默认情况下,sequelize假定您的表中有时间戳.这可以全局禁用

new Sequelize(..., { define: { timestamps: false }});
Run Code Online (Sandbox Code Playgroud)

或者每个型号:

sequelize.define(name, attributes, { timestamps: false });
Run Code Online (Sandbox Code Playgroud)

或者,如果您只有一些时间戳(fx已更新,但未创建)

sequelize.define(name, attributes, { createdAt: false });  
Run Code Online (Sandbox Code Playgroud)

如果您的列被调用其他内容:

sequelize.define(name, attributes, { createdAt: 'make_at' });
Run Code Online (Sandbox Code Playgroud)

http://docs.sequelizejs.com/en/latest/api/sequelize/

通过这种方式,您不必每次都指定所有属性 - sequelize知道它实际可以选择哪些属性.

如果您确实想要指定默认情况下应选择哪些属性,则可以使用范围

sequelize.define(name, attributes, { defaultScope { attributes: [...] }});
Run Code Online (Sandbox Code Playgroud)

这将适用于每个查找呼叫