【发布时间】:2017-11-11 15:23:55
【问题描述】:
我试图了解 Loopback 中的关系是如何工作的。假设有一个比赛模型,一场比赛有两支球队(主队和客队)。我是这样定义的:
{
"name": "Game",
"plural": "games",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"gameId": {
"type": "number",
"required": true,
"id": true
},
},
"validations": [],
"relations": {
"homeTeam": {
"type": "belongsTo",
"model": "Team",
"foreignKey": ""
},
"awayTeam": {
"type": "belongsTo",
"model": "Team",
"foreignKey": ""
},
},
"acls": [],
"methods": {}
}
所以主队和客队各自属于一个球队对象。这是我的团队模型:
{
"name": "Team",
"plural": "teams",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"teamId": {
"type": "number",
"required": true,
"id": true
},
"validations": [],
"relations": {
"games": {
"type": "hasMany",
"model": "Game",
"foreignKey": "teamId"
}
},
"acls": [],
"methods": {}
}
所以一个团队可以有很多场比赛。这对我来说听起来是正确的。当我转到http://localhost:3000/api/games/1871811/homeTeam 时,它正确显示了我在那场比赛中的主队。客队也一样。但是当我转到http://localhost:3000/api/teams/53/games 时,它只回复[]。我很确定 id 是正确的。
我在这里做错了什么?
【问题讨论】:
-
在游戏模型上,我认为您需要拥有主队 id 和客队 id 的属性,以便您可以映射这两个不同的模型
标签: loopbackjs loopback