Loopback HasManyThrough关系只能单向工作

时间:2016-05-09 00:14:28

标签: node.js loopbackjs strongloop

我有两个模型“Jobdetails”和“Jobvenues”,它们通过“JobDetailsHasVenues”模型具有“HasManyThrough”关系

Jobdetails模型:

{
 "name": "Jobdetails",
 "properties": {
"jobname": {
  "type": "String",
  "required": true,
  "length": 250,
  "precision": null,
  "scale": null,
  "mysql": {
        ---
     },
  "_selectable": false
}
},
"relations": {
"jobvenues": {
  "type": "hasMany",
  "model": "Jobvenues",
  "foreignKey": "jobdetailsId",
  "through": "JobdetailsHasVenues"
}
}

和Jobvenues模型为:

{
"name": "Jobvenues",
"properties": {
"storename": {
  "type": "String",
  "required": true,
  "length": 200,
  "precision": null,
  "scale": null,
  "mysql": {
   ----
   },
  "_selectable": false
}
}
"relations": {
"jobdetails": {
  "type": "hasMany",
  "model": "Jobdetails",
  "foreignKey": "venueId",
  "through": "JobdetailsHasVenues"
   }
}

直通模型定义为

 {
  "name": "JobdetailsHasVenues",
   {
    "jobdetailsid": {
     "type": "Number",
     "required": true,
    "length": null,
    "precision": 10
      ---,
    "venueid": {
    "type": "Number",
    "required": true,
    "length": null,
    "precision": 10
     ----
     },
    },
"relations": {
    "jobdetail": {
    "type": "belongsTo",
    "model": "Jobdetails",
    "foreignKey": "jobdetailsId"
   },
   "jobvenue": {
   "type": "belongsTo",
   "model": "Jobvenues",
   "foreignKey": "venueId"
  }
}

当我查询

    Jobdetails.find({
      filter:{
        where:{and:[{'status':{neq:3}},{'id':{neq:jobId}}]},
        include:'jobvenues'
          }
       })

结果中不存在职位。 但如果我查询

 Jobvenues.find({
      filter:{
        where:{venueid:jobdetailsId}}]},
        include:'jobdetails'
          }
       })

结果我可以看到相关的工作细节......

我提到了许多帖子,包括Strong Loop

上的文档

此处Stackoverflow

但是我的代码无法正常工作.....

还有一个观察......如果我通过Loopback Explorer查询并查询 GET / Jobdetails / {id} / jobvenues - 我可以看到相关的职位。

但是如果在查询GET / Jobdetails / {id}中使用“包含过滤器”,我会得到空的一系列作业。

1 个答案:

答案 0 :(得分:1)

尝试添加密钥

"jobvenues": {
  "type": "hasMany",
  "model": "Jobvenues",
  "foreignKey": "jobdetailsId",
  "through": "JobdetailsHasVenues",
  "keyThrough": "venueId"
}


"jobdetails": {
  "type": "hasMany",
  "model": "Jobdetails",
  "foreignKey": "venueId",
  "through": "JobdetailsHasVenues",
  "keyThrough": "jobdetailsId"
}