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

Count issue with related models using though model #1795

Open
regevbr opened this issue Nov 24, 2019 · 4 comments
Open

Count issue with related models using though model #1795

regevbr opened this issue Nov 24, 2019 · 4 comments
Assignees
Labels
bug community-contribution Patches contributed by community good first issue LB3-only Bugs affecting LoopBack 3 users only (not LoopBack 4+)

Comments

@regevbr
Copy link

regevbr commented Nov 24, 2019

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.

    "books": {
      "type": "hasMany",
      "model": "Book",
      "foreignKey": "userId",
      "through": "UserToBook",
      "keyThrough": "bookId"
    },

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 function
https://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

@regevbr regevbr added the bug label Nov 24, 2019
regevbr added a commit to regevbr/loopback-datasource-juggler that referenced this issue Nov 24, 2019
@regevbr
Copy link
Author

regevbr commented Nov 24, 2019

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
You can understand and base the fix based on the changes there

@regevbr
Copy link
Author

regevbr commented Nov 26, 2019

@dhmlau can you please take a look at this one?

@bajtos
Copy link
Member

bajtos commented Nov 29, 2019

@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 count and anther for avoid unnecessary include queries described in #1796. Just don't forget to add some tests to verify your fix and prevent regressions in the future.

@regevbr
Copy link
Author

regevbr commented Nov 30, 2019

Sure working on it right now

regevbr added a commit to regevbr/loopback-datasource-juggler that referenced this issue Nov 30, 2019
regevbr added a commit to regevbr/loopback-datasource-juggler that referenced this issue Nov 30, 2019
regevbr added a commit to regevbr/loopback-datasource-juggler that referenced this issue Nov 30, 2019
regevbr added a commit to regevbr/loopback-datasource-juggler that referenced this issue Nov 30, 2019
regevbr added a commit to regevbr/loopback-datasource-juggler that referenced this issue Nov 30, 2019
regevbr added a commit to regevbr/loopback-datasource-juggler that referenced this issue Nov 30, 2019
regevbr added a commit to regevbr/loopback-datasource-juggler that referenced this issue Nov 30, 2019
regevbr added a commit to regevbr/loopback-datasource-juggler that referenced this issue Nov 30, 2019
regevbr added a commit to regevbr/loopback-datasource-juggler that referenced this issue Nov 30, 2019
regevbr added a commit to regevbr/loopback-datasource-juggler that referenced this issue Nov 30, 2019
regevbr added a commit to regevbr/loopback-datasource-juggler that referenced this issue Nov 30, 2019
regevbr added a commit to regevbr/loopback-datasource-juggler that referenced this issue Nov 30, 2019
regevbr added a commit to regevbr/loopback-datasource-juggler that referenced this issue Nov 30, 2019
regevbr added a commit to regevbr/loopback-datasource-juggler that referenced this issue Dec 7, 2019
regevbr added a commit to regevbr/loopback-datasource-juggler that referenced this issue Dec 7, 2019
@dhmlau dhmlau added the community-contribution Patches contributed by community label Jan 21, 2020
@bajtos bajtos added the LB3-only Bugs affecting LoopBack 3 users only (not LoopBack 4+) label Sep 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug community-contribution Patches contributed by community good first issue LB3-only Bugs affecting LoopBack 3 users only (not LoopBack 4+)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants