在“字段列表”中将未知列“* .createdAt”
我在“字段列表”中收到未知列“userDetails.createdAt”当尝试使用关联进行提取时。
使用findAll
无关联工作正常。
我的代码如下:
var userDetails = sequelize.define('userDetails', { userId :Sequelize.INTEGER, firstName : Sequelize.STRING, lastName : Sequelize.STRING, birthday : Sequelize.DATE }); var user = sequelize.define('user', { email: Sequelize.STRING, password: Sequelize.STRING }); user.hasOne(userDetails, {foreignKey: 'userId'}); user.findAll({include: [userDetails] }).success(function(user) { console.log(user) });
我认为错误是你在sequelize中启用了时间戳,但是你在DB中的实际表定义不包含一个timestamp列。
当你做user.find它只会做SELECT user.*
,它只取得你实际拥有的列。 但是,当您连接时,连接表的每一列都将被别名,从而创build以下查询:
SELECT `users`.*, `userDetails`.`userId` AS `userDetails.userId`,`userDetails`.`firstName` AS `userDetails.firstName`,`userDetails`.`lastName` AS `userDetails.lastName`, `userDetails`.`birthday` AS `userDetails.birthday`, `userDetails`.`id` AS `userDetails.id`, `userDetails`.`createdAt` AS `userDetails.createdAt`, `userDetails`.`updatedAt` AS `userDetails.updatedAt` FROM `users` LEFT OUTER JOIN `userDetails` AS `userDetails` ON `users`.`id` = `userDetails`.`userId`;
解决办法是禁用userDetails模型的时间戳:
var userDetails = sequelize.define('userDetails', { userId :Sequelize.INTEGER, firstName : Sequelize.STRING, lastName : Sequelize.STRING, birthday : Sequelize.DATE }, { timestamps: false });
或所有型号:
var sequelize = new Sequelize('sequelize_test', 'root', null, { host: "127.0.0.1", dialect: 'mysql', define: { timestamps: false } });