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

Wrong count query for findAndCountAll with include #7225

Closed
pola88 opened this issue Feb 8, 2017 · 8 comments
Closed

Wrong count query for findAndCountAll with include #7225

pola88 opened this issue Feb 8, 2017 · 8 comments
Labels

Comments

@pola88
Copy link
Contributor

pola88 commented Feb 8, 2017

Hi guys, I updated to the version 3.30.2 and when I try to do findAndCountAll with include, the count query is wrong, example:

Product.findAndCountAll({ include: [ { model: Feature, as: 'features' }]});

I get:

SELECT count("Product"."id") AS "count" FROM "products" AS "Product" LEFT OUTER JOIN ("products_features" AS "features.products_features" INNER JOIN "features" AS "features" ON "features"."id" = "features.products_features"."feature_id") ON "Product"."id" = "features.products_features"."product_id";

expected:

SELECT count(*) AS "count" FROM "products" AS "Product";

if I have a product with more than 2 features, it will be counted twice

Sequelize: 3.30.2
dialect: pg

@ggrimbert
Copy link

If you specify an include, it will be passed to the count query, that's how it works.

If you don't want the JOIN, don't pass the include in the findAndCountAll

@kingjerod
Copy link

Pass distinct: true into the query and it will work how you want it to. It will still have the included data.

@stale
Copy link

stale bot commented Jun 29, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

@arvinhubert
Copy link

image

Wrong count with where clause

@stale stale bot removed the stale label Jun 30, 2017
@stale stale bot added the stale label Aug 29, 2017
@stale
Copy link

stale bot commented Aug 29, 2017

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

@stale stale bot closed this as completed Sep 5, 2017
@magmel48
Copy link

magmel48 commented Feb 7, 2018

Personally I think LEFT JOIN's should not be here as well. Count of rows cannot be changed if we use this type of JOIN or just omit it, but result query will be faster then.

@dalepo
Copy link

dalepo commented Mar 13, 2019

I think making this into a separate query would be a better approach since you might not want to use distinct in your query, it can lead into unwanted performance issues.

@mltsy
Copy link

mltsy commented Aug 11, 2023

For posterity: Using a separate query here seems like the right thing to do IF you're concerned about performance. with findAndCountAll, Sequelize is already performing two queries (a SELECT count(...) and a SELECT table.x, ...). The counting issue exists because the "includes" are joined to both of these queries. By using distinct: true the count query turns into SELECT count(DISTINCT(table.id)) as "count" but still joins all the included tables, and then collapses them by table.id - not the most efficient way to do that. It would be great if there were a way to "not include the includes" for the count query, but I don't know of a way to do that. However, you can emulate findAndCountAll, including its async nature, with something like this:

async function findAndCountApples(appleType)
  const where = { apply_type: appleType }
  const asyncRows = appleModel.findAll({
    where,
    include: ...,
    offset: ...
    limit: ...
  })
  const asyncCount = model.count({where})
  return Promise.all([asyncRows, asyncCount]).then(([rows, count]) => ({rows, count}))
}

Of course, if the full query with all joins is not an expensive/slow query, using distinct:true works just fine!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants