[英]How to set up a db one-to-many relationship in both ways?
For example, one team has N members, but one member could be in N teams.例如,一个团队有 N 个成员,但一个成员可以在 N 个团队中。 How can I express this both-way relationship in django?如何在 Django 中表达这种双向关系? or in Databases in general?还是在一般的数据库中?
Maybe I'm thinking about it wrong, but on the frontend I want to show all teams for example, or the teams of a player...Don't I need this?也许我想错了,但在前端我想展示所有的球队,或者一个球员的球队......我不需要这个吗? Thanks谢谢
This is a man-to-many (M:N) relationship and is usually modeled using a linking table.这是一种人对多 (M:N) 关系,通常使用链接表建模。 Here is an example:下面是一个例子:
teams
--------
team_id
team_name
persons
--------
person_id
person_name
team_members
---------
person_id
team_id
The team_members
table keeps the relations between teams and persons. team_members
表保持团队和个人之间的关系。 A person is a member of a team when a corresponding record with its person_id
and the team_id
exists in the team_members
table.当team_members
表中存在与其person_id
和team_id
对应的记录时,一个人就是团队的成员。 Now the team can hold as many members as necessary (M) and each person can be member of more than one team (N).现在,团队可以根据需要容纳任意数量的成员 (M),并且每个人可以成为多个团队 (N) 的成员。 And here comes the name of the relationship M:N.这就是关系 M:N 的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.