Loopback relation in non id field

I want to indicate the relationship between 2 mssql tables. Paymentcategory and payments. paymentcategory.id is attached to the payout.category column.

in the payout.json model, I indicated as foreignKey: id,

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

but loopback looks by default for id field as primaryKey

Is it possible to specify a connection in the category field. Preferably in the file common / models / payout.json?

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

Now I get this error:

"error": {
"name": "Error",
"status": 400,
"message": "Key mismatch: Paymentpayout.id: undefined, Paymentcategory.id: 1",
"statusCode": 400,
+4
source share
2 answers

You can define your foreign key as you like (in /common/models/your-model-name.json.

For more information, see my example at https://github.com/strongloop/loopback-example-relations-basic .

0
source

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.

0

All Articles