-
Notifications
You must be signed in to change notification settings - Fork 362
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
Count issue with related models using though model #1795
Comments
Since I'm using (sadly) loopback 2.x I have added the fix to my own fork - https://github.com/regevbr/loopback-datasource-juggler/blob/2.x/lib/scope.js |
@dhmlau can you please take a look at this one? |
@regevbr IIUC, this issue is related to #1796 in the sense that your commit regevbr@2cf8269#diff-c2a679b29102d5574291e884531cb8aeL135 is fixing both of them. Is that right? Your problem description look very reasonable to me, your fix looks sensible too. Would you like to contribute a fix yourself? It would be best to open two pull requests, one for fixing |
Sure working on it right now |
Steps to reproduce
Create a model called Book with the properties id,author,title
Add a hasMany relation between the default User model and the Book model using a through Model called UserToBook which has the following properties: userId,bookId
The relation is defined in the User model like so.
Current Behavior
Querying the user for his books using a where filter works as expected (there is a bug there as well which I will address in a different issue). The reason it works is the
related
functionhttps://github.com/strongloop/loopback-datasource-juggler/blob/d54d76947724d75a0f46b151c064742c678b0bbe/lib/scope.js#L61
Which, after querying the through model, also queries the related model directly.
When trying to count the number of books of a user, using a where filter (on the author for example), the result is the entire number of books the user has and not the number of books from that author. The reason for that is that the count function
https://github.com/strongloop/loopback-datasource-juggler/blob/d54d76947724d75a0f46b151c064742c678b0bbe/lib/scope.js#L509-L528
Only queries the though model, and not the related model directly.
Expected Behavior
Return the actual count, taking the filter under account
Link to reproduction sandbox
Working on it
Additional information
It seems that the findOne,updateAll and destroyAll suffer from the same issue
The text was updated successfully, but these errors were encountered: