是否有可能在 Sequelize 中通过关联包含.. 我已经尝试过像这样显示的代码,但它返回错误...
类型错误:无法读取未定义的属性“包含”
我正在使用 sql 方言
let instructorGrade = await model.Instructor.findOne({
where: { id },
attributes: ['id'],
include: [
{
model: model.SchoolGrade,
as: 'instructorSchoolGrades',
through: {
include: [
{
model: model.Grade,
as: 'schoolGrades'
},
{
model: model.Section,
as: 'schoolGradeSection'
}
]
}
}
]
});
这是 github 问题,与我的相同,但我还没有找到任何解决方案 [1]:https://github.com/sequelize/sequelize/issues/10828
最佳答案
你应该使用Nested eager loading 。例如,您的查询可能类似于:
let instructorGrade = await model.Instructor.findOne({
where: { id },
attributes: ['id'],
include: [
{
model: model.SchoolGrade,
as: 'instructorSchoolGrades',
include: [
{
model: model.Grade,
as: 'schoolGrades'
},
{
model: model.Section,
as: 'schoolGradeSection'
}
]
}
]
});
关于javascript - Sequelize Include On Through 关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64890300/