result.count for findAndCountAll is incorrect with include
See original GitHub issueWhat 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:
- Created 7 years ago
- Reactions:1
- Comments:10 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@leohihimax and @andreabianco Have you tried passing
distinct: true
( withcol
, if required).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
FROMmodel1
ASModel1
LEFT OUTER JOINsubmodel1
ASSubModel1
ONModel1
.id
=SubModel1
.Model1Id
…