Sequelize Include On Through 协会

Sequelize Include On Through association

是否可以在sequelize中通过关联收录.. 我试过像这样显示的代码,但它 returns 错误...

TypeError: 无法读取未定义的 属性 'includes'

我正在使用 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'
                }
            ]

        }
    ]
});