我在续集方面遇到问题,我尝试使用findAll()从我的表中获取数据,但是有一个错误消息指出字段列表中的未知列Jobs.userId,我从未在模型或表中进行注册< / p>
我尝试使用findAndCountAll()但仍然存在相同的错误,我还尝试以相同的结果删除所有关联,到目前为止唯一的解决方法是将userId放在排除数组上
这是我的控制器
const jobData = await jobs.findAll({
attributes:{exclude:['updatedAt']},
include:[{
model:jobOwners,
as:'owner',
attributes:['id','profilePicture'],
include:[{
model:users,
as:'user',
attributes:['id','name'],
},{
model:districts,
as:'district',
attributes:['id','name']
}]
}]
});
这是我的工作模式
'use strict';
module.exports = (sequelize, DataTypes) => {
const jobs = sequelize.define('jobs', {
jobOwnerId:{
type:DataTypes.INTEGER,
allowNull:false
},
caption: {
type:DataTypes.STRING,
allowNull:false
},
description: {
type:DataTypes.TEXT,
allowNull:false
},
districtId: {
type:DataTypes.INTEGER,
allowNull:false
},
address: {
type:DataTypes.TEXT,
allowNull:false
},
poster: {
type:DataTypes.TEXT,
allowNull:true
},
contactPerson: {
type:DataTypes.STRING,
allowNull:false
}
}, {});
jobs.associate = function(models) {
jobs.belongsTo(models.jobOwners,{as:'owner',foreignKey:'jobOwnerId'})
jobs.belongsTo(models.districts,{as:'districts', foreignKey:'districtId'})
};
return jobs;
};
这是我的桌子
CREATE TABLE jobs (
id int(11) NOT NULL,
jobOwnerId int(11) NOT NULL,
caption varchar(255) NOT NULL,
description text,
districtId int(11) NOT NULL,
address text NOT NULL,
poster text,
contactPerson varchar(15) NOT NULL,
createdAt datetime NOT NULL,
updatedAt datetime NOT NULL
)
这是控制台上显示的查询结果
SELECT jobs.id, jobs.jobOwnerId, jobs.caption, jobs.description, jobs.districtId, jobs.address,
jobs.poster, jobs.contactPerson, jobs.createdAt, jobs.userId, owner.id AS owner.id, owner.profilePicture AS owner.profilePicture,
owner->user.id AS owner.user.id, owner->user.name AS owner.user.name, owner->district.id
AS owner.district.id, owner->district.name AS owner.district.name FROM jobs AS jobs
LEFT OUTER JOIN jobOwners AS owner ON jobs.jobOwnerId = owner.id LEFT OUTER JOIN users AS owner->user
ON owner.userId = owner->user.id LEFT OUTER JOIN districts AS owner->district ON owner.districtId = owner->district.id;
应该没有job.userId
答案 0 :(得分:0)
问题解决了,问题不是来自Jobs模型或Jobs Controller,而是来自用户模型,我创建了具有hasMany Jobs的关联,并且解决了这个问题。