简体   繁体   English

连接三个表的表代码

[英]join table code for three tables

What would be the most elegant and maintainable way to write join table for 3 other tables? 编写其他3个表的联接表的最优雅,最可维护的方法是什么?

Lets say I have three tables: School, Student, Teacher, all with primary keys (IDs). 可以说我有三个表:学校,学生,老师,都带有主键(ID)。

Student can attend many schools, have many teachers. 学生可以上很多学校,有很多老师。 Teachers can teach in many schools. 教师可以在许多学校任教。 Teacher can teach same student in different school (or not). 老师可以在不同的学校(或不可以)教同一位学生。

I seen many articles with Many-to-Many relationships between two tables, but cannot find a right way to deal with three tables,, 我看到很多文章在两个表之间具有多对多关系,但是找不到正确的方法来处理三个表,

Any suggestions welcome. 任何建议欢迎。

Firstly -in my opinion- you need to add something like the concept of Course . 首先,在我看来,您需要添加诸如Course概念的内容。 It is the relationship that tells you which Student s some Teacher teaches in some specific School . 正是这种关系告诉您Student Teacher在某所特定School教给哪个Student

I introduce it because I guess that not all Teacher s teaching in some School teach all Student s attending to that School . 我介绍它,因为我想,不是所有Teacher的教学在一些School教所有Student的主治到School

  • Course is kept on one School . Course在一所School保留。 School can have many Course s. School可以School许多Course (relationship) (关系)

  • Course has many Student s. Course有很多Student Studend attends to many Course s. Studend参加许多Course (relationship) (关系)

  • Teacher teach many Course s. Teacher教许多Course Course has one teacher. Course只有一名老师。 (relationship) (关系)

In this set there is one ManyToMany relationship namely Student - Course . 在这个集合中有一个ManyToMany关系,即Student - Course Course has ManyToOne relationships to Teacher & School . CourseTeacherSchoolManyToOne关系。

I would go with using an ORM to help you with these kinds of relationships. 我会使用ORM来帮助您建立这种类型的关系。 I think your relationship is many-to-many-through relationship. 我认为您的关系是多对多关系。

Here's an article showing a possible match to what you want. 这是一篇文章,显示可能与您想要的匹配。

Describing Relationships: Django's ManyToMany Through 描述关系:Django的ManyToMany Through

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM