gpt4 book ai didi

node.js - 加入两个以上的表在 Sequelize 中不起作用

转载 作者:行者123 更新时间:2023-12-03 22:32:47 24 4
gpt4 key购买 nike

我有两个模型用户,CandidateExperience,所以当我加入两个表时它不起作用。
我是否必须在用户模型中添加关联

module.exports = (sequelize, DataTypes) => {
const Users = sequelize.define('Users', {
name: {type: DataTypes.STRING},
lName: {type: DataTypes.STRING},
email: {type: DataTypes.STRING},
}, {
hooks: {},
underscored: true
});
Users.associate = function (models) {
Users.hasMany(models.CandidateDetails, {
as: 'CandidateDetails',foreignKey: 'userId',constraints: false
})
Users.hasMany(models.CandidateExperience, {
as: 'CandidateExperience',foreignKey: 'userId',constraints: false
})
};
return Users;
};
候选人体验模型
module.exports = (sequelize, DataTypes) => {
const CandidateExperience = sequelize.define('CandidateExperience', {
candidate_exp_id: {type: DataTypes.UUID,primaryKey: true},
companyName: { type: DataTypes.STRING},
designation: {type: DataTypes.STRING}
}, {
underscored: true
});
CandidateExperience.associate = function (models) {
CandidateExperience.belongsTo(models.Users, {
as: 'User',foreignKey: 'userId',constraints: false})
};
return CandidateExperience;
};
但如果我使用这个查询,它会给我错误
return models.Users.findAll({
where: {userType:'engineer'},
include: [
{ model: models.CandidateExperience, as: "experienceDetail" ,
required: false},
]
});

最佳答案

您正在使用别名“CandidateExperience”将用户与 CandidateExprience 相关联
但是您正在使用“exprienceDetail”调用 User.findAll()
尝试其中之一

Users.hasMany(models.CandidateExperience, {
as: 'experienceDetail',foreignKey: 'userId',constraints: false
})
Users.findAll({
where: {userType:'engineer'},
include: [
{
model: models.CandidateExperience,
as: "CandidateExperience",
required: false
}
]
});

关于node.js - 加入两个以上的表在 Sequelize 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65276976/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com