ExpressJS - Sequelize - 列丢失错误

ExpressJS - Sequelize - Column Missing Error

除了连接到特定查询的描述之外,我正在尝试正确查询适合我的 sequelize 查询的所有图像,但我收到 createdAt 列的错误,该列未找到在我的 table 中。如何指定要在查询中使用的列?

这里是查询(图案和颜色正确拉入查询):

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
        });

});

这是我的 table:

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;

这是错误:

   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)

您可以明确设置要在查询中检索的属性(列名)。例如,如果您想检索 idpatterncolorimageUrlimageSource 列,您可以使用以下代码:

Images.findAll({
    where : {
        pattern: req.params.pattern,
        color: req.params.color
    },
    attributes : ['id', 'pattern', 'color', 'imageUrl', 'imageSource']
})

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

new Sequelize(..., { define: { timestamps: false }});

或每个型号:

sequelize.define(name, attributes, { timestamps: false });

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

sequelize.define(name, attributes, { createdAt: false });  

如果您的专栏名称不同:

sequelize.define(name, attributes, { createdAt: 'make_at' });

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

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

如果您真的想指定哪些属性应该由 select编辑 默认你可以使用范围

sequelize.define(name, attributes, { defaultScope { attributes: [...] }});

这将应用于每个查找调用