【问题标题】:Two relations from same model loopback not working来自同一模型环回的两个关系不起作用
【发布时间】:2017-03-17 23:23:45
【问题描述】:

我有两个型号usersappointments

users 模型如下-

{
    "users": {
      "0": {
        "id": "1",
        "name": "test1",
        "role": "doctor"
      },
      "1": {
        "id": "2",
        "name": "test2",
        "role": "patient"
      },
      "2": {
        "id": "3",
        "name": "test3",
        "role": "support"
      }
    }
  }

现在在上面的模型中,如果角色是医生,我们称它为doctor_id,如果是patient,那么patient_id等等。

现在我的约会模式如下->

{
    "name": "appointments",
    "plural": "appointments",
    "base": "PersistedModel",
    "idInjection": true,
    "options": {
      "validateUpsert": true
    },
    "properties": {
      "appointmentDate": {
        "type": "date"
      },
      "appointmentTime": {
        "type": "string"
      },
      "doctorId": {
        "type": "string"
      },
      "patientId": {
        "type": "string"
      }
    },
    "validations": [],
    "relations": {
      "Doctor": {
        "type": "belongsTo",
        "model": "users",
        "foreignKey": "doctorId"
      },
      "Patient": {
        "type": "belongsTo",
        "model": "users",
        "foreignKey": "patientId"
      }
    },
    "acls": [],
    "methods": {}
  }

所以当我尝试GET 所有约会时,它不会从users 发送关系数据。如果我添加单个关系,它会按预期工作,但不能处理来自同一模型的多个关系。

提前致谢,

【问题讨论】:

  • 按照我的做法,我相信外键应该是“”。您不应该定义医生 ID 和患者 ID,您可能必须在用户类中使用外键“医生”和“患者”定义两个 hasMany 关系
  • @user3802077:谢谢你的回复,但无法得到你真正想表达的内容,你能举例说明一下吗?

标签: node.js mongodb loopbackjs loopback-address


【解决方案1】:

包括我之前的评论以提供上下文:

我这样做的方式,我相信外键应该是 "" 两者。您不应该定义医生 ID 和患者 ID,您可能必须在用户类中使用外键“医生”和“患者”定义两个 hasMany 关系

这里举个例子是我的用户类(称为客户)中定义的关系

"commentsWritten": {
  "type": "hasMany",
  "model": "comment",
  "foreignKey": "sourceCustomerId"
},
"commentsReceived": {
  "type": "hasMany",
  "model": "comment",
  "foreignKey": "targetCustomerId"
},

然后在我的评论定义中我有以下内容

"properties": {
  ...
  "targetCustomerId": {
    "type": {
      "required": true
    }
  },
  "sourceCustomerId": {
    "type": {
      "required": true
    }
  }
},    
"relations": {
  "targetCustomer": {
    "type": "belongsTo",
    "model": "customer",
    "foreignKey": ""
  },
  "sourceCustomer": {
    "type": "belongsTo",
    "model": "customer",
    "foreignKey": ""
  }
},

请注意,我确实定义了属性(id),但如果我没记错的话,这只是为了强制它们不为空,即你不应该需要它。

【讨论】:

    【解决方案2】:

    如果您go to the belongsTo documentation,您可以看到foreignKey 字段为空,因此您需要将其从您的关系定义中删除。

    您可能还想定义关系的另一端。

    define a hasMany relationusers hasMany appointments:

    {
      "name": "users",
      "base": "PersistedModel",
      ...
      "relations": {
        "doctors": {
          "type": "hasMany",
          "model": "appointments",
          "foreignKey": "doctorId"
        },
        "patients": {
          "type": "hasMany",
          "model": "appointments",
          "foreignKey": "patientId"
        },
      ...
    }
    

    但是,您尝试执行的操作可能不受支持(事实上,我什至不确定这对 LoopBack 是否有意义)。

    您可以check the documentation on polymorphic relations,即使这是一项正在进行的工作,也没有提到同时通过多个foreignKey 拥有Model A hasMany Model B

    LoopBack 需要有一些逻辑才能在第一个外键下搜索,如果没有找到,则在另一个外键下搜索,我不确定是否支持这种复杂的组合。

    建议

    为什么不定义两种模型,一种用于医生,一种用于患者,而是有两种不同的关系?您甚至可以go to the documentation on HasManyThrough relations,并且您可以看到一个示例,该示例模拟了与您正在尝试做的非常相似的事情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      • 2018-12-29
      相关资源
      最近更新 更多