我正在尝试使用sequelize 查询连接表:
这是模型:
db.client.belongsToMany(db.user, {
through: db.clientUser,
onDelete: 'cascade',
});
db.user.belongsToMany(db.client, {
through: db.clientUser,
});
这就是我想做的:
db.user.findAll({
where: {
group_id: 1,
},
include: [{
model: db.clientUser,
where: {
is_manager: 1,
}
}],
raw: true,
})
但是我收到以下错误:client_user is not associated to user!
知道这个问题的原因是什么吗?
您声明了以下关系:client
from user
through clientUser
。虽然迂腐,但它的抱怨在技术上是正确的:之间没有明确声明的关系client
and clientUser
。也不应该有:你的belongsToMany
关系应该照顾到这一点。可以调整您的查询以处理这种关系。
注意:我不知道在哪些表中找到 group_id 和 is_manager。它们可能需要进行洗牌。
db.user.findAll({
where: {
group_id: 1,
},
include: [{
model: db.client,
through: {
where: {
is_manager: 1, // Assuming clientUser.is_manager?
},
}],
raw: true,
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)