Skip to content

findAndCountAll returns correct count but incorrect number of rows with query into association #9940

Open

Description

@doc82

What are you doing?

Running a findAndCountAll through an association, returns back the correct count - but the incorrect number of rows.

    const theRequest = {
      where,
      order: [['createdAt', sortOrder]],
      limit,
      offset: dbUtil.getPageOffset(page, limit),
      distinct: true,
      include : []
    };
    const templateInclude = {
      model: Template,
      // paranoid: false,
      required: true,
      where: { forSale: { [Op.eq]: true } }
    };
    if (req.query.type) {
      templateInclude.where.type = { [Op.eq]: req.query.type };
    }
    theRequest.include.push(templateInclude);

    const result = await Product.findAndCountAll(theRequest);

What do you expect to happen?

We expect a count of 1 and there to be 1 row returned

What is actually happening?

We get back a count of 1, with 2 rows.

I increased the amount of data into our dev environment, and it looks like the count is getting the correct query, but the data returned back looks like the query was ignored

Dialect: postgres
__Dialect version: "pg": "^7.4.3"
__Sequelize version: "sequelize": "^4.38.1"

Note : Your issue may be ignored OR closed by maintainers if it's not tested against latest version OR does not follow issue template.

Activity

  1. doc82 commented on Oct 9, 2018

    @doc82
    Author

    Looks like this is happening with any where statements attached to an included association :(

  2. doc82 commented on Oct 15, 2018

    @doc82
    Author

    Found out the issue: if I do a query into an association with a limit + offset it breaks. If I remove the limit and offset it works great

  3. NaifousBS commented on Oct 24, 2018

    @NaifousBS
  4. webindustry54 commented on Nov 7, 2018

    @webindustry54
  5. doc82 commented on Feb 7, 2019

    @doc82
    Author
  6. scottmay2020 commented on Nov 27, 2019

    @scottmay2020

    I made subquery false in my pagination function:
    const paginate = (query, { page, pageSize }) => { const currentPage = parseInt(page, 10) - 1 || 0; const currentPageSize = parseInt(pageSize, 10) || 25; const offset = currentPage * currentPageSize; const limit = offset + currentPageSize; return { ...query, offset, limit, // subQuery: false, }; };

    It seemed to work as I had an "activity" model includes that had many rows itself and when the parent model count would be seven, only one row would be returned holding the 71 rows of activity included. Making subquery false, returns all seven rows as expected.

  7. spinlud commented on Jan 9, 2020

    @spinlud
    Contributor

    Same issue:

    image

  8. deleted a comment from stale on Jan 17, 2020
  9. deleted a comment from stale on Jan 17, 2020
  10. reopened this on Jan 17, 2020
  11. added
    topic: count
    type: bug
    on Jan 17, 2020
  12. alikhanoitan commented on Jan 26, 2022

    @alikhanoitan

    I made subquery false in my pagination function
    @scottmay2020 I don't know what exactly the subQuery field does (will have a look later) but this solved the same issue described here. I tried almost the whole day. Thanks!

  13. rodrigorodriguez commented on Dec 15, 2023

    @rodrigorodriguez
  14. added theissue type on Mar 9, 2025
  15. removed
    type: bug
    on Mar 9, 2025
  16. jajo-shubham commented on Sep 2, 2025

    @jajo-shubham

    Using subQuery: false doesnt work when I have left join + limit. Basicaly the limit should be applied on the original model before left joining but it is being applied on the whole joined result set. It works perfectly in findAll with subQuery: false but not findAndCountAll

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

    topic: count

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions