非id字段的环回关系

时间:2014-11-07 13:12:54

标签: loopbackjs

我想指定2个mssql表之间的关系。 Paymentcategory和支付。 paymentcategory.id加入payout.category列。

在payout.json模型中 我指定为foreignKey:id,

"relations": {
    "paymentcategories": {
      "type": "hasOne",
      "model": "Paymentcategory",
      "foreignKey": "id"
 }

但是,对于id字段,loopback默认显示为primaryKey

有没有办法在类别字段中指定连接。 最好在common / models / payout.json文件中?

"relations": {
    "paymentcategories": {
      "type": "hasOne",
      "model": "Paymentcategory",
      "foreignKey": "id",
      "primaryKey": "category" ??????????????????
 }

现在我收到了这个错误:

"error": {
"name": "Error",
"status": 400,
"message": "Key mismatch: Paymentpayout.id: undefined, Paymentcategory.id: 1",
"statusCode": 400,

2 个答案:

答案 0 :(得分:0)

您可以将外键定义为您想要的任何内容(在/common/models/your-model-name.json

有关详细信息,请参阅https://github.com/strongloop/loopback-example-relations-basic上的示例。

答案 1 :(得分:0)

您的Paymentcategory模型应该是

{
  "name":"Paymentcategory",
  "options":{...},
  "properties":{
  "id":{...},
  ...
  },
  "relations":{
    "payouts":{
      "type":"hasMany",
      "model":"Payout",
      "foreignKey":"category"
    }
  }
}

请注意,一个付款类别可能包含该类别的N个付款(这是分类的目的)。

您的付款模式应为

{
  "name":"Payout",
  "options":{...},
  "properties":{
    "id":{...},
    "category":{...},
    ...
  },
  "relations":{
    "paymentcategories":{
      "type":"belongsTo",
      "model":"Paymentcategory",
      "foreignKey":"category"
    }
  }
}

需要注意的重要事项是:两个关系对象都具有相同的foreignKey,并且它是Payout.category。