javascript - Sequelize Include On Through 关联

标签 javascript sql sequelize.js

是否有可能在 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/

相关文章:

javascript - 测试中的 NestJS 全局模块

mysql - 无法过滤掉记录

sql - psql onDelete : cascade not working 的 Sequelize

javascript - 如何不在 ExtJS HTTPRequest 中传递 cookie

javascript - ReactJS onChange 函数

sql - 使用 3 个表连接获取 API 列表

mysql - sql error using between operator with date between two dates

javascript - Sequelize : Many To Many table(CROSS TABLE) associated to other table

javascript - Sequelize 查询 SELECT * FROM Table 只返回一行

javascript - 无需提交即可获取文本字段值(html)