Ellebkey Ellebkey -4 years ago 449
Javascript Question

Sequelize Find belongsToMany Association

I have an association m:n between two tables with sequelize like this:

Course

module.exports = function(sequelize, DataTypes) {

var Course = sequelize.define('Course', {
.....
},
{
associate: function(models){
Course.hasMany(models.Schedule);
Course.belongsTo(models.Period);
Course.belongsTo(models.Room);
Course.belongsTo(models.Subject);
Course.belongsTo(models.School);
Course.belongsTo(models.Person, { as: 'Teacher' });
}
}
);
return Course;
};


Person

module.exports = function(sequelize, DataTypes) {

var Person = sequelize.define('Person', {
....
},
{
associate: function(models){
Person.belongsTo(models.Role, { as: 'Role' });
Person.belongsTo(models.School, { as: 'School' });
Person.belongsTo(models.Person, { as: 'Tutor' });
}
}
);

return Person;
};


And the association table Enrollment

module.exports = function(sequelize, DataTypes) {

var Enrollment = sequelize.define('Enrollment', {
....
},
{
associate: function(models){
Enrollment.belongsTo(models.Product, {as: 'Product'});
Enrollment.belongsTo(models.School, { as: 'School' });

models.Person.belongsToMany(models.Course, {through: {model: Enrollment},foreignKey: 'StudentEnrollId'});
models.Course.belongsToMany(models.Person, {through: {model: Enrollment},foreignKey: 'CourseEnrollId'});

}
}

);
return Enrollment;
};


I tried following this "example" but doesn't explain much rather than a simple query where include the parameter through.

What I trying to archive is to get All the courses given a Student id (Person Model). As you can see the course model only saves the id of differents tables that together form a course. The Person Model also is associate to differents models so I give a custom id name with
foreignKey: 'StudentEnrollId'
but when I try to specify the id name in the include
model : db.Person, as: 'StundetEnroll'
the query show the following error:
Person (StudentEnroll) is not associated to Course

Answer Source

You need to define the alias as also in the belongsToMany association

models.Person.belongsToMany(models.Course, { as: 'CourseEnrolls', through: { model: Enrollment }, foreignKey: 'StudentEnrollId'});
models.Course.belongsToMany(models.Person, { as: 'StudentEnrolls', through: { model: Enrollment }, foreignKey: 'CourseEnrollId'});

Now you will be able to query Course with all it's students and vice-versa

models.Course.findByPrimary(1, {
    include: [
        {
            model: models.Person,
            as: 'StudentEnrolls'
        }
    ]
}).then(course => {
    // course.StudentEnrolls => array of Person instances (students of given course)
});

You can also use get/set Associations methods in order to retrieve or set associated objects

// assuming that course is an instance of Course model
course.getStudentEnrolls().then(students => {
    // here you get all students of given course
});
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download