javascript - Sequelize Find belongsToMany Association - Stack Overflow

I have an association m:n between two tables with sequelize like this:Coursemodule.exports = function(s

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

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

Share Improve this question asked Mar 22, 2017 at 17:44 EllebkeyEllebkey 2,3113 gold badges25 silver badges31 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745481738a4629587.html

相关推荐

  • javascript - Sequelize Find belongsToMany Association - Stack Overflow

    I have an association m:n between two tables with sequelize like this:Coursemodule.exports = function(s

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信