You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
count() returns wrong number when including 'include' #1517
When running count or findAndCountAll together with an included relation, the results coming back are actually counting the related rows instead of the primary rows.
In my example I have the model User which hasMany(Project).
Creating one user and three projects and adding the three projects to the user works as expected. Then, when doing findAndCountAll for the user's id, the count comes back as 3, when I expect it to come back as 1.
I think we could add "options.include = null;" in dao-factory.js:613 but I'm not sure if it might break other functionality.
Also, I think the test cases for this feature is wrong, more specifically
"[MYSQL] Include findAndCountAll should include associations to findAndCountAll"
and probably also "DAOFactory findAndCountAll handles offset with includes"
Oh sorry, I meant that they seem to make the wrong assumptions regarding what the total count should be when fetching related instances (e.g. 3 instead of 1). But to be honest I didnt spend too much time investigating the test cases so dont worry too much about that. :)
@alum ah in that case please do correct the test case - pretty sure i wrote it heh.
Can you add some assertions to your test gist so i can try to fix it? We <3 self contained code, and we love pull requests with unit tests even more ;)
findAndCountAll with includes is a really complex case because of the subquery logic involved with complicated includes.
We'd love more test cases for it to stabilize it completely.
Activity
eshell commentedon Mar 19, 2014
i have this problem as well. haven't gotten to test it yet to try and find out what its actually counting. just giving a +1
mickhansen commentedon Mar 19, 2014
"The test cases are wrong" - How so? And please provide better ones in a PR.
But yeah counts are tricky with includes, so not unlikely there's a bug.
alum commentedon Mar 19, 2014
I was merely pointing out the relevant test cases. Will get back with a PR when I have some time to look into it.
mickhansen commentedon Mar 20, 2014
Yes but you said they are wrong, how are they wrong? Do you mean incomplete or not covering all cases?
alum commentedon Mar 21, 2014
Oh sorry, I meant that they seem to make the wrong assumptions regarding what the total count should be when fetching related instances (e.g. 3 instead of 1). But to be honest I didnt spend too much time investigating the test cases so dont worry too much about that. :)
mickhansen commentedon Mar 21, 2014
@alum ah in that case please do correct the test case - pretty sure i wrote it heh.
Can you add some assertions to your test gist so i can try to fix it? We <3 self contained code, and we love pull requests with unit tests even more ;)
findAndCountAll with includes is a really complex case because of the subquery logic involved with complicated includes.
We'd love more test cases for it to stabilize it completely.
alum commentedon Mar 21, 2014
Gist updated. :)
gzurbach commentedon Apr 18, 2014
Hey there! I'm encountering the same issue and I was wondering if the solution could be to simply add a 'DISTINCT' in the query?
select count(distinctusers.id) ...It might be a very naive proposition but it did the trick for me :)
youxiachai commentedon May 3, 2014
You can try use
RichAyotte commentedon May 3, 2014
@gzurbach @youxiachai For the correct count, the conditions of the inner joined tables need to be considered as well.
mickhansen commentedon Dec 3, 2014
Likely fixed by 82403cf - else please open new issue with specific test case.