Javascript 环回多个一对多关系
我已经开始用Loopback弄脏我的手,我对模型关系声明有点困惑 示例模型Javascript 环回多个一对多关系,javascript,loopbackjs,Javascript,Loopbackjs,我已经开始用Loopback弄脏我的手,我对模型关系声明有点困惑 示例模型 Person firstname lastname idcountrybirth//引用国家表 idcountrynational//再次引用国家表 请注意,IDCountryBorth和IDCountryNational可以为每个人引用不同的国家 上述关系 我一直在为两个国家/地区中的每一个寻找类似于hasA的东西,但这种关系并不存在 我需要为国家建立什么样的人际关系 下面是我当前ERD的一个示例模型,我正试图基于
Person
firstname
lastname
idcountrybirth//引用国家表
idcountrynational//再次引用国家表
IDCountryBorth
和IDCountryNational
可以为每个人引用不同的国家
上述关系
我一直在为两个国家/地区中的每一个寻找类似于hasA的东西,但这种关系并不存在
我需要为国家建立什么样的人际关系
下面是我当前ERD的一个示例模型,我正试图基于环回对其进行修改
您可以为每个对象使用一个hasOne
关系。不要将IDCountryBorth
和IDCountryNational
定义为属性,因为它们都表示个人
和国家
之间的关系
{
"name": "Person",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"firstname": {
"type": "string"
},
"lastname": {
"type": "string"
}
},
"validations": [],
"relations": {
"birthCountry": {
"type": "hasOne",
"model": "Country",
"foreignKey": "birthCountryId"
},
"nationality": {
"type": "hasOne",
"model": "Country",
"foreignKey": "nationalityCountryId"
},
},
"acls": [],
"methods": []
}
然后,使用restapi
POST /api/Person
{
"firstname": "R.",
"lastname" : "Federer"
}
然后,给他指定一个出生国
POST /api/Person/1/birthCountry
{
"name": "Switzerland"
}
和国籍
POST /api/Person/1/nationality
{
"name": "South Africa"
}
有些人有多个国籍,因此您可以使用hasMany
关系而不是hasOne
来表示国籍
关系;)