Loopback: Where filter not working in hasManyThrough relation in Loopback 3

Created on 12 Jul 2017  路  12Comments  路  Source: strongloop/loopback

Context

I recently migrated an application from Loopback 2 to Loopback 3. After that process, queries involving a where filter on a hasManyThrough relation does not work anymore: they always return an empty array.

My application uses a mssql datasource, but I reproduced this behaviour on a minimal project using the memory datasource.

Description/Steps to reproduce

Create a new Loopback 3 application using loopback-cli. Then, reproduce the models of the official documentation for hasManyThrough relations :

  • add 3 models (Physician, Patient, Appointment)
  • add hasManyThrough relations between Physician and Patient
  • add belongsTo relations between Appointment and Physician/Patient

With a simple test set, I get following results:

loopback > phy = Physician.findById(2)
{ name: 'Dr. Strangelove', id: 2 }
loopback > phy.patients({})
[ { name: 'Mickey', id: 2 }, { name: 'Minnie', id: 3 } ]
loopback > phy.patients({where: {name: 'Minnie'}})
[]

Although there is a patient related to the physician that matches the where filter, the result of the last query is empty. Note that I used loopback-console to get this output, but the API explorer gives the same results.

Expected result

When reproducing the above steps in a freshly loopback-cli-scaffolded Loopback 2 project, I get the following results :

loopback > phy = Physician.findById(2)
{ name: 'Dr. Strangelove', id: 2 }
loopback > phy.patients({})
[ { name: 'Mickey', id: 2 }, { name: 'Minnie', id: 3 } ]
loopback > phy.patients({where: {name: 'Minnie'}})
[ { name: 'Minnie', id: 3 } ]

This time, the last query anwers with a list containing the one patient related to the physician that matches the where filter, as expected.

Additional information

node -e 'console.log(process.platform, process.arch, process.versions.node)'

linux x64 6.11.0

yarn list --depth=0 | grep loopback

鈹溾攢 [email protected]
鈹溾攢 [email protected]
鈹溾攢 [email protected]
鈹溾攢 [email protected]
鈹溾攢 [email protected]
鈹溾攢 [email protected]
鈹溾攢 [email protected]
鈹溾攢 [email protected]
鈹溾攢 [email protected]
鈹溾攢 [email protected]
鈹溾攢 [email protected]

lb --version

2.6.0 ([email protected] [email protected])

yarn --version

0.27.5

All 12 comments

Closing, since it should have been reported in strongloop/loopback-datasource-juggler.

In fact, an issue was reported, and a fixed is proposed, by @f0rmiga, see
strongloop/loopback-datasource-juggler#1406

hi @dromaludaire I am reviewing PR https://github.com/strongloop/loopback-datasource-juggler/pull/1406, and traced your issue here.

The PR is linked to a closed issue, while we usually open a new issue to describe the reproduce steps, track discussion and fix, the comments under PR should only be related to the code review.

Would you mind if I reopen this issue and continue the discussion here?

Thank you for reproducing it @dromaludaire.

If you need anything else from me, please feel free to ask @jannyHou.

hi @jannyHou ,

There are ofc no problem if you reopen this issue. (sorry for the delay, i was on vacation)

I am having the same issue, is there a solution or workaround to this?

@adelriosantiago This regression was introduced in loopback-datasource-juggler 3.7.0. Therefore, you have two workarounds:

  1. edit your package.json so as to use loopback-datasource-juggler=3.6.1 (latest working version)
  2. apply the fix proposed by @f0rmiga in strongloop/loopback-datasource-juggler#1406

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Looks like this will be closed when PR strongloop/loopback-datasource-juggler#1522 lands.

@dromaludaire Thanks for reminding. Closing it.

@jannyHou I'm already in version 3.13.0 and I'm still unable to query related models when using hasManyThrough

a very simple query won't work

json { "where": {"email":"[email protected]"} }

We have had this issue for some time now, I was expecting strongloop/loopback-datasource-juggler#1522 fixed it but the problem remains.

This is a pretty basic query not working, are you guys having the same issue?

@jonathan-casarrubias https://github.com/strongloop/loopback-datasource-juggler/pull/1522 indeed fixes this issue, but it was committed on Nov, 14, ie. after 3.13.0 was released. You can either wait for a new version of datasource-juggler to be released, or use the master branch.

Im using

roleMapping model has following relations

"role": {
  "type": "belongsTo",
  "model": "role",
  "foreignKey": "roleId"
},
"user": {
  "type": "belongsTo",
  "model": "user",
  "foreignKey": "principalId"
}

user model has

"roles": {
  "type": "hasMany",
  "model": "role",
  "foreignKey": "principalId",
  "through": "roleMapping",
  "keyThrough": "roleId"
},

and role has

"users": {
  "type": "hasMany",
  "model": "user",
  "foreignKey": "roleId",
  "through": "roleMapping",
  "keyThrough": "principalId"
},

When im trying to get related users from role like

role.users.find(filter);

the filter will apply to roleMapping instead of user collection.
How to fix it?

Was this page helpful?
0 / 5 - 0 ratings