【问题标题】:What does "through model" mean in loopback?环回中的“通过模型”是什么意思?
【发布时间】:2015-10-24 17:17:35
【问题描述】:

使用“slc loopback:relation”定义关系时,在最后一行提示“through model”。

? Select the model to create the relationship from: CoffeeShop
? Relation type: has many
? Choose a model to create a relationship with: Reviewer
? Enter the property name for the relation: reviewers
? Optionally enter a custom foreign key:
? Require a through model? No

有人能简单解释一下什么是直通模型吗? 一些例子将不胜感激。

【问题讨论】:

    标签: loopbackjs strongloop


    【解决方案1】:

    直通模型一般用于many to many数据关系。 例如,您有 3 个模型:

    1. Useridusername 字段;
    2. TeamidteamName 字段;
    3. TeamMemberuserIdteamId 字段。

    User 可以是多个Team 的成员,Team 可以拥有多个User。而UserTeam的关系会存储在TeamMember中。

    要在环回中创建 many to many 关系,您必须将 relation 属性添加到模型定义文件中:

    1. User模型定义文件(user.json)中

    "relations": {
      "teams": {
        "type": "hasMany",
        "model": "team",
        "foreignKey": "userId",
        "through": "teamMember"
      }
    }
    1. Team模型定义文件中

    "relations": {
      "users": {
        "type": "hasMany",
        "model": "user",
        "foreignKey": "teamId",
        "through": "teamMember"
      }
    }
    1. TeamMember模型定义文件中

    "relations": {
      "user": {
        "type": "belongsTo",
        "model": "user",
        "foreignKey": "userId"
      },
      "team": {
        "type": "belongsTo",
        "model": "team",
        "foreignKey": "teamId"
      }
    }

    您还可以在 StrongLoop 的docs 上找到有关“通过模型”的信息

    【讨论】:

    猜你喜欢
    • 2019-01-25
    • 2017-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-03
    • 1970-01-01
    • 2020-01-05
    相关资源
    最近更新 更多