问题描述
我在两个表之间有一个关联m:n,如下所示:
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;
};
和关联表注册
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.人员模型还与不同模型相关联,因此我使用foreignKey: 'StudentEnrollId'
给出了一个自定义ID名称,但是当我尝试在包含model : db.Person, as: 'StundetEnroll'
中指定ID名称时,查询显示以下错误: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
});
这篇关于续集查找属于多对关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!