定义多对一关系(many-to-one)
多对一会在many
一方的实体表中生成一个外键指向one
的实体表。
极简模式
假设存在两个实体Org
和User
,声明关系User many-to-one Org
:
Org.json
{
"fields" : {
"id" : "uuid identity"
...
}
}
User.json
{
"fields" : {
...
"orgId" : "-> Org"
...
}
}
通过在外键字段中定义-> {Target Entity}
就可以了,字段类型会自动复制到外键字段中。
外键字段默认会被设置为required
,设置为可选需要增加optional
属性:
"orgId" : "-> Org optional"
多次引用同一个实体
ObjectType
:
{
"fields" : {
"id": "uuid identity"
}
}
Object
-> ObjectType
:
- Unnamed
{
"fields": {
"primaryTypeId": "-> Type",
"secondaryTypeId" : "-> Type optional"
}
}
可以通过
optional
指定关系可选(外键字段允许null
)
- Named
{
"fields": {
"primaryTypeId": "-> Type primaryType",
"secondaryTypeId" : "-> Type secondaryType"
}
}
primaryType
和secondaryType
是关系名(relation name)
引用复合主键的实体
Entity1
:
{
"fields": {
"id1" : "varchar(100) identity",
"id2" : "varchar(100) identity",
}
}
Entity2
-> Entity1
:
{
"fields" : {
"entity1Id1" : "-> Entity1.id1 relation1",
"entity1Id2" : "-> Entity1.id2 relation1"
}
}
引用复合主键的实体需要指定引用的字段
{Entity}.{id_field}
。
设置expandable
{
"fields": {
"orgId": "-> Org expandable"
}
}
设置queryable
Single 0.6.0
{
"fields": {
"orgId": "-> Org expandable queryable"
}
}
多对一关系的queryable
默认为false
,需要明确声明。