Skip to content

count() returns wrong number when including 'include' #1517

Closed

Description

@alum

When running count or findAndCountAll together with an included relation, the results coming back are actually counting the related rows instead of the primary rows.

In my example I have the model User which hasMany(Project).
Creating one user and three projects and adding the three projects to the user works as expected. Then, when doing findAndCountAll for the user's id, the count comes back as 3, when I expect it to come back as 1.

I think we could add "options.include = null;" in dao-factory.js:613 but I'm not sure if it might break other functionality.

Also, I think the test cases for this feature is wrong, more specifically
"[MYSQL] Include findAndCountAll should include associations to findAndCountAll"
and probably also "DAOFactory findAndCountAll handles offset with includes"

Please see the following code example:
https://gist.github.com/alum/9630602#file-index-js

Not sure if I'm missing something regarding counts...? :)

Activity

  1. eshell commented on Mar 19, 2014

    @eshell

    i have this problem as well. haven't gotten to test it yet to try and find out what its actually counting. just giving a +1

  2. mickhansen commented on Mar 19, 2014

    @mickhansen
    Contributor

    "The test cases are wrong" - How so? And please provide better ones in a PR.
    But yeah counts are tricky with includes, so not unlikely there's a bug.

  3. alum commented on Mar 19, 2014

    @alum
    ContributorAuthor

    I was merely pointing out the relevant test cases. Will get back with a PR when I have some time to look into it.

  4. mickhansen commented on Mar 20, 2014

    @mickhansen
    Contributor

    Yes but you said they are wrong, how are they wrong? Do you mean incomplete or not covering all cases?

  5. alum commented on Mar 21, 2014

    @alum
    ContributorAuthor

    Oh sorry, I meant that they seem to make the wrong assumptions regarding what the total count should be when fetching related instances (e.g. 3 instead of 1). But to be honest I didnt spend too much time investigating the test cases so dont worry too much about that. :)

  6. mickhansen commented on Mar 21, 2014

    @mickhansen
    Contributor

    @alum ah in that case please do correct the test case - pretty sure i wrote it heh.
    Can you add some assertions to your test gist so i can try to fix it? We <3 self contained code, and we love pull requests with unit tests even more ;)

    findAndCountAll with includes is a really complex case because of the subquery logic involved with complicated includes.

    We'd love more test cases for it to stabilize it completely.

  7. alum commented on Mar 21, 2014

    @alum
    ContributorAuthor

    Gist updated. :)

  8. gzurbach commented on Apr 18, 2014

    @gzurbach

    Hey there! I'm encountering the same issue and I was wondering if the solution could be to simply add a 'DISTINCT' in the query?

    select count(distinct users.id) ...

    It might be a very naive proposition but it did the trick for me :)

  9. youxiachai commented on May 3, 2014

    @youxiachai
    Contributor

    You can try use

    Model.find({
      attributes [sequelize.fn('count', sequelize.fn('DISTINCT', sequelize.col('id')))
    })
  10. RichAyotte commented on May 3, 2014

    @RichAyotte

    @gzurbach @youxiachai For the correct count, the conditions of the inner joined tables need to be considered as well.

  11. mickhansen commented on Dec 3, 2014

    @mickhansen
    Contributor

    Likely fixed by 82403cf - else please open new issue with specific test case.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions