我正在尝试使用Sequelize从关联表中获取数据
- 父表[主表]
- 学校表[保存所有学校信息]
- ParentSchool表[交叉引用表]
我正在尝试通过选择学校名称来过滤数据,作为回报,我需要json结果用于续集FINDALL以返回与该学校关联的所有父母。
{
//includes the cross-reference table to join the schools with parents
include: [
{ model: db.parentSchools },
{
model: db.schools, as: "schools",
where: {
[Op.eq]: [{ name: req.params.school }]
}
}
]
}
).then(function (results) {
res.json(results);
console.log("parents for a school ", results);
})
排序关联语法
Parent.belongsToMany(models.schools, {
as: "schools",
through: "parentSchools",
foreignKey: "parentId"
});
};
两种协会
// a school belongs to many parents
School.belongsToMany(models.parents, {
as: "parents",
// cross-reference table because a parent can have many schools as well
through: "parentSchools",
foreignKey: "schoolId"
});
返回一个json对象,其中包含输入的学校名称的值。