【问题标题】:Loopback HasManyThrough relation working only one wayLoopback HasManyThrough 关系仅以一种方式工作
【发布时间】:2016-09-03 12:33:43
【问题描述】:

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

职位详情模型:

{
 "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'
          }
       })

结果我可以看到相关的jobdetails.....

我参考了很多帖子,包括Strong Loop 的文档

这里Stackoverflow

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

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

但如果在查询 GET /Jobdetails/{id} 中使用“包含过滤器”,我会得到空的工作场所数组。

【问题讨论】:

    标签: node.js loopbackjs strongloop


    【解决方案1】:

    尝试添加键通

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多