Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hasMany through - filter/where/order not working #166

Closed
fabien opened this issue Jul 8, 2014 · 47 comments
Closed

hasMany through - filter/where/order not working #166

fabien opened this issue Jul 8, 2014 · 47 comments

Comments

@fabien
Copy link
Contributor

fabien commented Jul 8, 2014

var Space = app.loopback.getModel('Space');
var User = app.loopback.getModel('User');
var Account = app.loopback.getModel('Account');

Space.hasAndBelongsToMany('users', { through: Account });
Space.hasMany('accounts');

User.hasAndBelongsToMany(Space, { as: 'spaces', through: Account });
User.hasMany('accounts');

Example: /api/spaces/client-a/users?filter[where][username]=toby // result: []
Example: /api/spaces/client-a/users?filter[order]=username // result: unordered array

Regarding sorting, since models are fetched individually (byId vs. find), ordering is not applied either. This might very well be a limitation of how juggler handles relations at the moment (very flexible regarding mixed datasources, but not optimal performance wise).

I think the best approach would be to gather the related ids from the through-model first, then issue a second query on the actual target model, scoped to the previously retrieved ids - this is not only more efficient, but should enable the filtering capabilities as expected.

@lchenay
Copy link
Contributor

lchenay commented Jul 24, 2014

When using hasAndBelongsToMany of hasManyThrought and making a find on relation (with api or with model) filter apply to the throught model and not on the destination model.

This is a huge problem.

@bajtos
Copy link
Member

bajtos commented Jul 24, 2014

Possibly related: #94 #85

@fabien fabien self-assigned this Sep 6, 2014
@mrjrdnthms
Copy link

+1

@glesage
Copy link

glesage commented Nov 10, 2014

+1 indeed! (to @lchenay 's comment) - and I'm interested in both hasAndBelongsToMany and hasManyThrought, just fyi

Thanks!

@janober
Copy link

janober commented Nov 10, 2014

+1 also did just run into the same issue with hasAndBelongsToMany. Wanted to use "fields".

@glesage
Copy link

glesage commented Nov 14, 2014

FYI, for now I'm JSON stringifying and then JSON parsing the results of my queries... very inneficient but works:

MyModel.find({include: "my_relation"}, function (err, myModels)
 {
    myModels = JSON.parse(JSON.stringify(myModels));
    // then when you call an individual result's.my_relation you actually get what you want
}

@superkhau
Copy link
Contributor

When using hasAndBelongsToMany of hasManyThrought and making a find on relation (with api or with model) filter apply to the throught model and not on the destination model.

Possible solution at #476

@superkhau
Copy link
Contributor

@raymondfeng Is this related to strongloop/loopback/issues/1076?

@raymondfeng
Copy link
Contributor

@superkhau I think so. Can you verify?

@superkhau
Copy link
Contributor

.../api/spaces/client-a/users?filter[where][username]=toby // result: []

@fabien What is client-a in this example?

@kjdelisle kjdelisle assigned kjdelisle and unassigned jannyHou Nov 25, 2016
@kjdelisle kjdelisle removed their assignment Dec 1, 2016
@jannyHou
Copy link
Contributor

jannyHou commented Dec 8, 2016

Hi @raymondfeng, the fix is published in 3.x and I am not sure should I backport it to 2.x?
I feel the filter would be good to have consistent behaviour in 2.x and 3.x, but the change itself is a bit breaking lol.
Any idea?

@raymondfeng
Copy link
Contributor

@jannyHou What are the breaking changes? How much effort is needed to backport to 2.x?

@kristojorg
Copy link

I believe this may be still broken in 3.x . I have a TopUser extended from User which hasMany Role, but the response of GET on /TopUser/:id/roles does not show any roles.

@jannyHou
Copy link
Contributor

@kristojorg could you fork https://github.com/strongloop/loopback-sandbox and provide me your code? Thanks.
From your description, it's hard to tell is it broken by this fix or some other factors, for example, this issue is specific for relation hasManyThrough not hasMany, and some connector could also affect the behavior.
With your sample app I can work with you to figure out how to get roles for a topuser.

@ilkerc
Copy link

ilkerc commented Jul 7, 2017

+1

@sidewaiise
Copy link

Wow this is a big problem. {where} filter does not map to target model. I'd like to fix it in a fork.

Have looked at the libraries - wouldn't it make sense to have RelationDefinition.hasManyThrough() instead of trying to cram the through into hasMany? Seems it would be easier to manage from a library perspective.

Also please confirm - is the problem persisting in hasAndBelongsToMany relation? We did not use that because our datasource couldn't find the correct join table and/or relation labelings.

@jannyHou
Copy link
Contributor

jannyHou commented Jul 18, 2017

The fix is already released in 2.55.0 and also released long time ago in [email protected], otherwise tests won't pass:
https://github.com/strongloop/loopback-datasource-juggler/blob/master/test/relations.test.js#L726

Could anyone provide me a sandbox that reproduce the problem? see #166 (comment)

@ilkerc
Copy link

ilkerc commented Jul 19, 2017

@jannyHou I tried. Couldn't reproduce. I'm using Mongo ObjectId's in every id field including reference id fields in relational tables. I think it's somehow related to this. If I catch anything related with mongo connector, shall I continue reporting here ?

@jannyHou
Copy link
Contributor

Thank you @ilkerc, if it's a mongodb specific problem, then https://github.com/strongloop/loopback-connector-mongodb would be the best place to open issue and discuss.

@mbenedettini
Copy link

@jannyHou as far as I can see, those tests you mentioned only test the case when filter is an id. If I change the where condition to be, for example, name or any other attribute, then the query stops working.

@jannyHou
Copy link
Contributor

jannyHou commented Aug 10, 2017

@mbenedettini Thank you I think it's caused by same reason as issue strongloop/loopback#3487 reported.
And there is a PR fix opened for it #1406

I am going to close this issue since the original bug is fixed in [email protected], the failures you observe is caused by another bug.
The conversation happens here is a little misleading :( I find some similar issues opened for new bug in loopback and loopback-datasource-juggler, will post another story to track the new bug&fix in the comment later.
Thanks for understanding.

@jannyHou
Copy link
Contributor

Let's keep discussion in issue strongloop/loopback#3487, which should reflect the filter issue you run into recently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests