Loopbackjs 非id字段上的环回关系
我想指定两个mssql表之间的关系。 付款类别和付款。 paymentcategory.id在payout.category列上联接 在payout.json模型中 我指定为foreignKey:idLoopbackjs 非id字段上的环回关系,loopbackjs,Loopbackjs,我想指定两个mssql表之间的关系。 付款类别和付款。 paymentcategory.id在payout.category列上联接 在payout.json模型中 我指定为foreignKey:id "relations": { "paymentcategories": { "type": "hasOne", "model": "Paymentcategory", "foreignKey": "id" } 但是,默认情况下,环回将id字段视为pr
"relations": {
"paymentcategories": {
"type": "hasOne",
"model": "Paymentcategory",
"foreignKey": "id"
}
但是,默认情况下,环回将id字段视为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,
您可以将外键定义为您想要的任何内容(在
/common/models/your model name.json
中)
有关更多信息,请参见我的示例。您的Paymentcategory模型应该是
{
"name":"Paymentcategory",
"options":{...},
"properties":{
"id":{...},
...
},
"relations":{
"payouts":{
"type":"hasMany",
"model":"Payout",
"foreignKey":"category"
}
}
}
{
"name":"Payout",
"options":{...},
"properties":{
"id":{...},
"category":{...},
...
},
"relations":{
"paymentcategories":{
"type":"belongsTo",
"model":"Paymentcategory",
"foreignKey":"category"
}
}
}
请注意,一个支出类别可能有N个与该类别相关的支出(这就是分类的目的)
你的支付模式应该是
{
"name":"Paymentcategory",
"options":{...},
"properties":{
"id":{...},
...
},
"relations":{
"payouts":{
"type":"hasMany",
"model":"Payout",
"foreignKey":"category"
}
}
}
{
"name":"Payout",
"options":{...},
"properties":{
"id":{...},
"category":{...},
...
},
"relations":{
"paymentcategories":{
"type":"belongsTo",
"model":"Paymentcategory",
"foreignKey":"category"
}
}
}
需要注意的重要一点是:两个关系对象具有相同的foreignKey,并且是Payout.category