我在我的服务器控制器上有以下查询,其中我为我的课程提供了来自al关联表的所有数据:
db.Course.findAll({
include: [
// {model:db.Room, attributes:['id','DisplayLabel']},
// {model:db.Period, attributes:['id','DisplayName']},
{model:db.Subject, attributes:['id','Name']},
// {model:db.Schedule, attributes:['id','Day','StartHour','EndHour']},
{model:db.Person, where : { id : db.Course.PersonId}},
]}
课程模型关联是这样的:
Course.hasMany(models.Schedule);
Course.belongsTo(models.Period);
Course.belongsTo(models.Room);
Course.belongsTo(models.Subject);
Course.belongsTo(models.School);
Course.belongsTo(models.Person);
问题在于我无法获得" Person"数据导致sql日志显示连接一直通过与Person Model关联的其他表:
verbose: Executing (default): SELECT `Course`.`id`, `Course`.`Scholarship`, `Course`.`Level`, `Course`.`CourseType`, `Course`.`RecordStatus`, `Course`.`DeletedAt`, `Course`.`createdAt`, `Course`.`updatedAt`, `Course`.`PeriodId`, `Course`.`RoomId`, `Course`.`SubjectId`, `Course`.`SchoolId`, `Course`.`PersonId`, `Course`.`CreatedById`, `Course`.`UpdateById`, `Course`.`DeleteById`, `Subject`.`id` AS `Subject.id`, `Subject`.`Name` AS `Subject.Name`, `People`.`id` AS `People.id`, `People`.`RFC` AS `People.RFC`, `People`.`FirstName` AS `People.FirstName`, `People`.`LastName` AS `People.LastName`, `People`.`LastName2` AS `People.LastName2`, `People`.`Phone` AS `People.Phone`, `People`.`Cellphone` AS `People.Cellphone`, `People`.`Email` AS `People.Email`, `People`.`Birthday` AS `People.Birthday`, `People`.`RecordStatus` AS `People.RecordStatus`, `People`.`DeletedAt` AS `People.DeletedAt`, `People`.`createdAt` AS `People.createdAt`, `People`.`updatedAt` AS `People.updatedAt`, `People`.`RoleId` AS `People.RoleId`, `People`.`SchoolId` AS `People.SchoolId`, `People`.`TutorId` AS `People.TutorId`, `People`.`CreatedById` AS `People.CreatedById`, `People`.`UpdateById` AS `People.UpdateById`, `People`.`DeleteById` AS `People.DeleteById`, `People.Task`.`Title` AS `People.Task.Title`, `People.Task`.`Content` AS `People.Task.Content`, `People.Task`.`Grade` AS `People.Task.Grade`, `People.Task`.`OtherDetails` AS `People.Task.OtherDetails`, `People.Task`.`TaskType` AS `People.Task.TaskType`, `People.Task`.`RecordStatus` AS `People.Task.RecordStatus`, `People.Task`.`DeletedAt` AS `People.Task.DeletedAt`, `People.Task`.`createdAt` AS `People.Task.createdAt`, `People.Task`.`updatedAt` AS `People.Task.updatedAt`, `People.Task`.`KardexId` AS `People.Task.KardexId`, `People.Task`.`PersonId` AS `People.Task.PersonId`, `People.Task`.`CourseId` AS `People.Task.CourseId`, `People.Task`.`CreatedById` AS `People.Task.CreatedById`, `People.Task`.`UpdateById` AS `People.Task.UpdateById`, `People.Task`.`DeleteById` AS `People.Task.DeleteById` FROM `Courses` AS `Course` LEFT OUTER JOIN `Subjects` AS `Subject` ON `Course`.`SubjectId` = `Subject`.`id` INNER JOIN (`Tasks` AS `People.Task` INNER JOIN `People` AS `People` ON `People`.`id` = `People.Task`.`PersonId`) ON `Course`.`id` = `People.Task`.`CourseId` AND `People`.`id` = NULL;
就像你可以看到它带来了Task模型中的列,这是一个源自Course和People之间的m:n关系的表
associate: function(models){
models.Person.belongsToMany(models.Course, {through : Task});
models.Course.belongsToMany(models.Person, {through : Task});
}
此外,如果我仅仅为了调试目的从数据库中删除该表,它与另一个m:n关系也是如此。如何从联接中将其排除以获取数据?
模型人,您是否想知道它会保存所有不同的用户:学生,教师,父亲,并且它具有角色模型的外键,具有此RoleTypes。这就是为什么我在课程模型上有PersonId键,在这种情况下是教师,在任务模型中,例如,PersonId指的是学生用户。
答案 0 :(得分:1)
您的查询使用join
表格Task
,因为您在Person
和Course
之间定义了两个关联。第一个是Course.belongsTo(Person)
,第二个是m:n关系。会发生什么是Sequelize考虑其中一个 - 如何知道你想要belongsTo
关联而不是m:n?为了省略这种情况,您可以在关联中使用alias
Course.belongsTo(models.Person, { as: 'Teacher' }); // example alias
然后,为了使用Course
检索Teacher
,您必须包含alias
db.Course.findAll({
include: [
{ model: db.Person, as: 'Teacher' }
]
}).then((course) => {
// course with Teacher
});
您可以使用m:n关系
执行相同的操作models.Course.belongsToMany(models.Person, { as: 'Students', through: Task });
然后你会使用Students
别名来返回给定课程的所有学生。
修改强>
要获取ModelId
以外的列名,您需要在关联中定义foreignKey
或otherKey
。对于Course
和Person
之间的m:n关系,您希望StudentId
代替PersonId
,您必须使用otherKey
models.Course.belongsToMany(models.Person, { as: 'Students', through: Task, otherKey: 'StudentId' });
现在,连接表将包含CourseId
(引用Course
表)和StudentId
(引用Person
表)。
注意:如果您还在Person
模型上定义m:n关系,请记住添加等效选项 - foreignKey
,如下所示:
models.Person.belongsToMany(models.Course, { as: 'Courses', through: Task, foreignKey: 'StudentId' });