2
votes

I have 2 Models "Jobdetails" and "Jobvenues" which has a "HasManyThrough" relation Through Model "JobDetailsHasVenues"

Jobdetails Model:

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

And Jobvenues Model as :

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

The Through Model is defined as

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

When I query

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

The jobvenues are are not present in the result. But if I query

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

In result I can see related jobdetails .....

I have refered many posts including the documentation at Strong Loop

Here Stackoverflow

But cant get my code working right .....

One more observation... If I query through Loopback Explorer and query through GET /Jobdetails/{id}/jobvenues -- I can see the related jobvenues.

But if use "include filter" in query GET /Jobdetails/{id} I get empty array of jobvenues.

1

1 Answers

1
votes

try adding keythrough

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


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