我正在使用Sequelize和ExpressJS开发一个CRUD应用程序,该应用程序具有以下表:
Parents | Students | ParentStudents | id | id | id |
---|---|---|
名称 | 名称 | idParent |
idStudent |
一个变通办法是:
let parents = await models.parents.findAll();
for (let parent of parents) {
let idParent = parent.dataValues.id;
let students = await models.sequelize.query(`
SELECT * FROM students s
LEFT JOIN parentStudents ps
ON s.id = ps.studentId
WHERE ps.parentId =${idParent};
`);
parent.dataValues['students'] = students;
}