question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

result.count for findAndCountAll is incorrect with include

See original GitHub issue

What you are doing?

Hello, I’m trying to use findAndCountAll methods for list view pagination. I have different models but I can post one example.

sequelize.define('Model1');
sequelize.define('SubModel1');
sequelize.define('SubModel2');
sequelize.define('SubModel3');

Model1 is associated with SubModule1, SubModule2 and SubModules with hasMany relation.

Now in the database I have 15 Model1 items.

What do you expect to happen?

If I perform the findAndCountAll I expect to see the following response:

{ count: 15,
  rows: [...] }

What is actually happening?

But the real result is: { count: 33, rows: […] }

Dialect: mysql Database version: 5.6 Sequelize version: 3.23.2

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

9reactions
sushantdhimancommented, Aug 12, 2016

@leohihimax and @andreabianco Have you tried passing distinct: true ( with col, if required).

2reactions
andreabiancocommented, Aug 16, 2016

I tested with Sequelize v. 3.19.3 and it’s working correctly. I update to the version 3.24.0 but it generate an invalid query.

SELECT count(DISTINCT(*)) AS count FROM model1 AS Model1 LEFT OUTER JOIN submodel1 AS SubModel1 ON Model1.id = SubModel1.Model1Id

Read more comments on GitHub >

github_iconTop Results From Across the Web

Wrong count query for findAndCountAll with include #7225
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:...
Read more >
Sequelize query returning the wrong number of rows when ...
query.limit = 12). This works fine if I don't include any associated rows, but when I include the related models it returns 7...
Read more >
The Comprehensive Sequelize Cheatsheet
Sequelize Models are ES6 classes. We can easily add custom instance or class level methods. const ModelName = sequelize.
Read more >
db.collection.count() — MongoDB Manual
On a sharded cluster, db.collection.count() without a query predicate can result in an inaccurate count if orphaned documents exist or if a chunk...
Read more >
sequelize query use column name instead model name
The count part of findAndCountAll incorrectly generates the distinct reference: filter from the scope is correctly used, but the reference is ambiguous ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found