javascript - Sequelize 查找 belongsToMany 关联

标签 javascript mysql node.js associations sequelize.js

我在两个表之间有一个 m:n 关联,像这样 Sequelize :

类(class)

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

我尝试遵循这个“example”但没有解释太多,而是一个包含参数的简单查询。

我试图存档的是获取所有给定学生 ID(人员模型)的类(class)。如您所见,类(class)模型仅保存共同构成类(class)的不同表的 ID。 Person 模型也关联到不同的模型,因此我使用 foreignKey: 'StudentEnrollId' 给出自定义 ID 名称,但是当我尝试在 include model 中指定 id 名称时:db.Person, as: 'StundetEnroll' 查询显示以下错误:Person (StudentEnroll) is not associated to Course

最佳答案

您还需要在 belongsToMany 关联中定义别名 as

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,反之亦然

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 方法来检索或设置关联对象

// assuming that course is an instance of Course model
course.getStudentEnrolls().then(students => {
    // here you get all students of given course
});

关于javascript - Sequelize 查找 belongsToMany 关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42959073/

相关文章:

node.js - NodeJS/Electron 关于客户端/服务器代码的混淆

javascript - 如何使用 JavaScript Promise 提示异步操作?

javascript - Angular js,如何在选择更改时启用

javascript - C# 和 Javascript 代码的计算有什么区别

php - 管理员连接两个用户

c# - 更新mysql数据库导致错误

php - ZF2 select 中的别名 - 带有计算字段

javascript - 卡住 req.flash 不是函数错误

javascript - 使用 Puppeteer 禁用检查元素

javascript - 不会使用nodeJS将数据插入到mongoDB "users"集合