Musíte definovat alias as
také v belongsToMany
sdružení
models.Person.belongsToMany(models.Course, { as: 'CourseEnrolls', through: { model: Enrollment }, foreignKey: 'StudentEnrollId'});
models.Course.belongsToMany(models.Person, { as: 'StudentEnrolls', through: { model: Enrollment }, foreignKey: 'CourseEnrollId'});
Nyní budete moci zadat dotaz na Course
se všemi studenty a naopak
models.Course.findByPrimary(1, {
include: [
{
model: models.Person,
as: 'StudentEnrolls'
}
]
}).then(course => {
// course.StudentEnrolls => array of Person instances (students of given course)
});
Můžete také použít get/set Associations
metody k načtení nebo nastavení přidružených objektů
// assuming that course is an instance of Course model
course.getStudentEnrolls().then(students => {
// here you get all students of given course
});