我在'字段列表'中收到一个未知列'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)
});
答案 0 :(得分:160)
我认为错误是你在sequelize中启用了时间戳,但是你在DB中的实际表定义不包含时间戳列。
当你执行user.find时,它只会执行SELECT user.*
,它只会获取你实际拥有的列。但是当您加入时,联接表的每一列都会有别名,这会创建以下查询:
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
}
});
答案 1 :(得分:3)
从laravel迁移到featherjs时,我遇到了同样的错误。表具有列名created_at,updated_at而不是createdat,updatedat。我必须在Sequelize模型中使用字段名称映射,如下所示
const users = sequelize.define('users', {
id: {
type: Sequelize.INTEGER,
primaryKey: true
},
createdAt: {
field: 'created_at',
type: Sequelize.DATE,
},
updatedAt: {
field: 'updated_at',
type: Sequelize.DATE,
},
..
..
..
..
答案 2 :(得分:1)
好吧,也许为时已晚,但是您可以像
那样在迁移时创建createdAt,updatedAt createdAt: {
allowNull: false,
defaultValue: Sequelize.fn('now'),
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
defaultValue: Sequelize.fn('now'),
type: Sequelize.DATE
}
我使用Express和Sequelize MySQL 然后模型就像正常定义 例如:
module.exports = (sequelize, DataTypes) => {
const Customer = sequelize.define('customer', {
name: DataTypes.STRING,
email: DataTypes.STRING,
phone: DataTypes.TEXT,
consider: DataTypes.TEXT,
otherQuestion: DataTypes.TEXT
}, {})
return Customer
答案 3 :(得分:0)
我遇到同样的错误,两种解决方法:
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'primary key',
`name` varchar(30) DEFAULT NULL COMMENT 'user name',
`created_at` datetime DEFAULT NULL COMMENT 'created time',
`updated_at` datetime DEFAULT NULL COMMENT 'updated time',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='user';
const Proje
ct = sequelize.define('project', {
title: Sequelize.STRING,
description: Sequelize.TEXT
},{
timestamps: false
})
答案 4 :(得分:0)
禁用时间戳 例如:-
const User = sequelize.define('user', {
firstName : Sequelize.STRING,
lastName : Sequelize.STRING,
}, {
timestamps: false
});
答案 5 :(得分:0)
对于Postgresql:
const sequelize = new Sequelize('postgres://user:pass@url:port/dbname',{
define:{
timestamps: false
}
})
不用说,用您的配置值替换user
,pass
,url
,port
和dbname
值。