Skip to content

findAndCount returns array instead of count when the query uses GROUP BY #9109

Open

Description

@jharting

What are you doing?

I am fetching a list of records (User) and counting the number of Projects each is associated with. I am using [sequelize.fn('COUNT', sequelize.col('projects.id')), 'project_count'] together with GROUP BY to do that in a single query without fetching whole project entities.

Full testcase can be found here: jharting@af2681a

What do you expect to happen?

I expect the count field in the result to return 3.

What is actually happening?

The count field contains an array: [ { count: 1 }, { count: 1 }, { count: 1 } ]

Dialect: mysql
Database version: 10.1.26
Sequelize version: 4.34.0
Tested with master branch: Yes

Activity

  1. added a commit that references this issue on Feb 26, 2018
  2. added
    type: bug
    on Apr 7, 2018
  3. tizzyapunkt commented on Aug 5, 2018

    @tizzyapunkt

    Currently facing the same issue. Any news on that?

  4. notflip commented on Dec 4, 2018

    @notflip

    Any news on this?

  5. keyiis commented on Feb 13, 2019

    @keyiis

    @jharting i facing the same issue. are you fixed?

  6. papb commented on Jul 30, 2019

    @papb
    Member

    @jharting Thanks for the testcase. Sorry to take so long for a response.

    I reproduced this in every dialect. Interestingly, every dialect gives a different failure.

    • SQlite: AssertionError: expected [ Array(3) ] to equal 3
    • MariaDB: SequelizeDatabaseError: (conn=10, no: 1054, SQLState: 42S22) Unknown column 'user.id' in 'field list'
    • MySQL: SequelizeDatabaseError: Unknown column 'user.id' in 'field list'
    • Postgres: SequelizeDatabaseError: missing FROM-clause entry for table "user"
    • MSSQL: SequelizeDatabaseError: Column 'Users.name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
  7. added
    status: understood
    on Jul 30, 2019
  8. jeebus3000 commented on Jan 10, 2020

    @jeebus3000

    Any progress on this?

  9. papb commented on Jan 17, 2020

    @papb
    Member

    @jeebus3000 none that I know of

  10. bentedder commented on Jul 12, 2020

    @bentedder

    Is there still no resolution on this?

  11. Hwan-seok commented on Sep 7, 2020

    @Hwan-seok

    Still... :(

  12. RahulRawat1994 commented on Sep 30, 2020

    @RahulRawat1994

    Any progress

  13. RahulRawat1994 commented on Sep 30, 2020

    @RahulRawat1994

    ? :(

  14. ritvikroy commented on Jan 4, 2021

    @ritvikroy

    Just use "result.count.length/count.length" instead "result.count/count" when you use Group by in findAndCountAll function.

  15. added theissue type on Mar 9, 2025
  16. removed
    type: bug
    on Mar 9, 2025
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

    status: understood

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions