简体   繁体   English

如何在MySQL(SQL)中为父对子对建模

[英]How to model parent to child pair in MySQL (SQL)

I have a data model that includes element types Stage, Actor, and Form. 我有一个数据模型,其中包括元素类型Stage,Actor和Form。 Logically, Stages can be assigned pairs of ( Form <---> Actor ) which can be duplicated many times (ie same person and same form added to the same stage at a later date/time). 逻辑上,可以为阶段分配成对的(Form <---> Actor)对,这些对可以重复多次(即,同一个人和相同的表单在以后的日期/时间添加到同一阶段)。

Right now I am modeling this with these tables: 现在,我正在使用以下表格对此进行建模:

Stage
Form
Actor

Form_Actor    
_______________
|Id           |
|FormId       | --> Id in Form
|ActorId      | --> Id in Actor


Stage_FormActor
__________________
|Id              |
|StageId         | --> Id in Stage
|FormActorId     | --> Id in Form_Actor

I am using CodeSmith to generate the data layer for this setup and none of the templates really know how to handle this type of relationship correctly when generating classes. 我正在使用CodeSmith来为此设置生成数据层,并且没有一个模板真正知道如何在生成类时正确处理这种类型的关系。 Ideally, the ORM would have Stage.FormActors where FormActor would be the pair Form, Actor. 理想情况下,ORM将具有Stage.FormActors,其中FormActor将是Form,Actor对。

Is this the correct way to model these relationships. 这是对这些关系建模的正确方法吗? I have tried using all three Ids in one table as well 我也尝试在一个表中使用所有三个ID

Stage_Form_Actor
______________
|Id          |
|StageId     | --> Id in Stage
|FormId      | --> Id in Form
|ActorId     | --> Id in Actor

This doesn't really get generated very well either. 这也不是很好。 Ideas? 有想法吗?

I don't know anything about CodeSmith, but your database schema for defining inherent many-to-many relationships between these three entities is correct (StageFormActor one is best). 我对CodeSmith一无所知,但是用于定义这三个实体之间固有的多对多关系的数据库架构是正确的(StageFormActor最好)。

One thing to note is that you might want to define a compound primary key to that association table (instead of using an artificial ID primary key) 有一点要注意的是,你可能希望定义一个复合主键,它关联表(而不是使用人工ID主键)

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

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