【问题标题】:Multiple relations to same class in LoopbackLoopback 中与同一类的多个关系
【发布时间】: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


【解决方案1】:

在游戏中,您不需要为其 id 设置属性。 loopback 会自动创建自增 id。游戏表应保存 awayTeamId 和 homeTeamId 以便您可以进行映射以获取从主场到客场或客场到主场的数据。
但是当您在关系外键上设置这些ID时,这些ID也会由环回创建

games.model

{
  "name": "games",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {

  },
  "validations": [],
  "relations": {
    "homeTeam": {
      "type": "belongsTo",
      "model": "homeTeam",
      "foreignKey": "homeTeamId",
      "options": {
        "disableInclude": false
      }
    },
    "awayTeam": {
      "type": "belongsTo",
      "model": "awayTeam",
      "foreignKey": "awayTeamId",
      "options": {
        "disableInclude": false
      }
    }

  },
  "acls": [],
  "methods": {}
}

离开,家庭模式

{
    "name": "awayTeam",
    "base": "PersistedModel",
    "idInjection": true,
    "options": {
      "validateUpsert": true
    },
    "properties": {
      "name": {
        "type": "string"
      }
    },
    "validations": [],
    "relations": {
      "games": {
        "type": "hasMany",
        "model": "games",
        "foreignKey": "awayTeamId",
        "options": {
          "disableInclude": false
        }
      }
    },
    "acls": [],
    "methods": {}
  }

主客场模式也一样
无需设置自己的ID,它会使用foreignKey引用homeTeamId/ awayTeamId

ps。如果您使用的是内存数据源,则每次更改代码并重新运行服务器时都需要放入数据。

【讨论】:

  • 我将 gameId 包含在模型中,因为我想使用我的 API 提供程序为游戏保留相同的 id。所以无论我的 API 调用返回什么,我都将其分配为游戏模型的 id。我必须为主队和客队提供不同的模型吗?我的意思是这可能是一个解决方案,但我觉得这会带来比它解决的问题更多的问题。顺便说一句,我没有使用内存数据源。
  • hm.. 或者它可能返回 [ ] 因为您放入团队模型的 id 实际上是主键 id 而不是 teamId ?实际上使用remoteMethod和调试它们会很容易
猜你喜欢
  • 1970-01-01
  • 2014-07-02
  • 2014-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-23
相关资源
最近更新 更多