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
findAndCountAll distinct with include but without include.where condition #2713
@PierrickP So to boil it down, distinct is needed with includes (sometimes), but illegal syntax without includes (joins)? So perhaps we should turn off distinct when there's no include needed.
/* * Copy this file to ./sscce.js * Add code from issue * npm run sscce-{dialect} */varSequelize=require('./index');varsequelize=require('./test/support').createSequelizeInstance();varM=sequelize.define('M'),N=sequelize.define('N');M.hasMany(N);sequelize.sync({force: true}).then(()=>M.findAndCountAll({distinct: true,include: [N]})).then(console.log).catch(console.error);
Output
Using database: sequelize-test-54Executing(default): SELECTcount(DISTINCT("M"."id"))AS"count"FROM"Ms"AS"M"LEFTOUTERJOIN"Ns"AS"Ns"ON"M"."id"="Ns"."MId";{count: 0,rows: []}
Executing (default): SELECT count(DISTINCT("M"."id")) AS "count" FROM "Ms" AS "M" LEFT OUTER JOIN "Ns" AS "Ns" ON "M"."id" = "Ns"."MId";
{ count: 0, rows: [] }
Executing (default): SELECT count(DISTINCT("M"."abc", "N"."xyzzy")) AS "count" FROM "Ms" AS "M" LEFT OUTER JOIN "Ns" AS "Ns" ON "M"."id" = "Ns"."MId";
{ count: 0, rows: [] }
Please Guide me regarding this. I am in a scenario where I get the wrong count just because of this missing functionality.
Activity
mickhansen commentedon Dec 11, 2014
options.includeis undefined forcountbecause it is not needed for the count, we filter it out on the latest release.I'm not exactly sure what's the issue here?
PierrickP commentedon Dec 17, 2014
The query is just invalid on sqlite / postgres (mysql not tested).
Rc2 seem to be ok for me! but with a different query (distinct with "id")
PierrickP commentedon Jan 9, 2015
Ok i understood everything.
I have 2 case.
When i want to findAndCountAll without include.where condition,
distinctare useless (and generate the bad query, maybe should be handle ?)Distinct are mandatory (for me) when you use include.where. because, without, count and rows.length arn't egal (see http://runnable.com/VK_-zN3jAYAH3UZq/bad-count-when-using-include-condition-for-node-js-and-sequelize not using distinct, count query count twice one dish beacause it has both tags!)
For my case, it's now all right. There are probably still an error when using
distinctwithfindAndCountAlland without include.wheremickhansen commentedon Jan 9, 2015
@PierrickP So to boil it down, distinct is needed with includes (sometimes), but illegal syntax without includes (joins)? So perhaps we should turn off distinct when there's no include needed.
mickhansen commentedon Jan 9, 2015
I would prefer that you open a PR with a failing unit test rather than a runnable, that would help us in a possible fix :)
BridgeAR commentedon Jun 30, 2016
I just checked the documentation and sqlite3 seems to allow the current behavior.
sushantdhiman commentedon Jul 6, 2016
Now Fixed
SSCCE Test
Output
aqdas95 commentedon Jul 3, 2019
Hi,
I understand that,
resolves to
but shouldn't there be an option to do
which would resolve to
Please Guide me regarding this. I am in a scenario where I get the wrong count just because of this missing functionality.
marianyfs commentedon Jul 3, 2019
up
Fix code generation for count() on distinct rows
fix(model): count() code generation for distinct rows
fix(model): count() code generation for distinct rows
fix(model): count() code generation for distinct rows
PokatilovArt commentedon Jun 21, 2021
I've setted these 2 properties
distinct: true, col: "MODEL_NAME.MODEL_PRIMARY_KEY"and it's worked for me