定义多对一关系(many-to-one)

多对一会在many一方的实体表中生成一个外键指向one的实体表。

极简模式

假设存在两个实体OrgUser,声明关系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"
    }
}

primaryTypesecondaryType是关系名(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,需要明确声明。

Bingosoft            updated 2018-11-01
上一篇:关系定义 下一篇:一对多关系

results matching ""

    No results matching ""