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