Sequelize Find belongsToMany 关联

我在两个表之间有一个关联 m:n 与这样的 sequelize:

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

课程

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

人物

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

以及关联表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.

我试图存档的是获取所有课程给定一个学生 ID(人模型).如您所见,课程模型仅保存共同构成课程的不同表的 id.Person 模型也与不同的模型相关联,因此我使用 foreignKey: 'StudentEnrollId' 提供了一个自定义 id 名称,但是当我尝试在包含 model : db.Person 中指定 id 名称时,如:'StudentEnroll' 查询显示以下错误:Person (StudentEnroll) is not associated to Course

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

推荐答案

你需要在belongsToMany关联中也定义别名as

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

现在您将能够查询 Course 及其所有学生,反之亦然

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

您还可以使用 get/set Associations 方法来检索或设置关联对象

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

相关文章