Sequelize Find belongsToMany Association

前提是你 提交于 2020-06-11 06:04:06

问题


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


回答1:


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
});


来源:https://stackoverflow.com/questions/42959073/sequelize-find-belongstomany-association

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!