Skip to content

findAndCountAll distinct with include but without include.where condition #2713

Closed

Description

@PierrickP

Something looks bad on these lines

if i try this kind of query

Dishes.findAndCountAll({
  distinct: true,
  include: [{
    model: Tags
  }]
});

The count query will be SELECT count(DISTINCT(*)) AS countFROMdishesASdish WHERE 1=1;
with an option.include to undefined at https://github.com/sequelize/sequelize/blob/master/lib/model.js#L837

Activity

  1. mickhansen commented on Dec 11, 2014

    @mickhansen
    Contributor

    options.include is undefined for count because 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?

  2. PierrickP commented on Dec 17, 2014

    @PierrickP
    Author

    The query is just invalid on sqlite / postgres (mysql not tested).

    ERROR:  syntax error at or near "*"
    LIGNE 1 : SELECT count(DISTINCT(*)) AS `count` FROM `dishes` AS `dish`...
    

    Rc2 seem to be ok for me! but with a different query (distinct with "id")

    Dishes.findAndCountAll({
            distinct: 'id',
            include: [{model: Tags}]
    })
  3. PierrickP commented on Jan 9, 2015

    @PierrickP
    Author

    Ok i understood everything.
    I have 2 case.

    When i want to findAndCountAll without include.where condition, distinct are 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 distinct with findAndCountAll and without include.where

  4. mickhansen commented on Jan 9, 2015

    @mickhansen
    Contributor

    @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.

  5. mickhansen commented on Jan 9, 2015

    @mickhansen
    Contributor

    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 :)

  6. BridgeAR commented on Jun 30, 2016

    @BridgeAR
    Contributor

    I just checked the documentation and sqlite3 seems to allow the current behavior.

  7. sushantdhiman commented on Jul 6, 2016

    @sushantdhiman
    Contributor

    Now Fixed

    SSCCE Test

    /*
     * Copy this file to ./sscce.js
     * Add code from issue
     * npm run sscce-{dialect}
     */
    
    var Sequelize = require('./index');
    var sequelize = require('./test/support').createSequelizeInstance();
    
    var M = 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-54
    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: [] }
  8. aqdas95 commented on Jul 3, 2019

    @aqdas95

    Hi,

    I understand that,

    sequelize.sync({ force: true })
      .then(() => M.findAndCountAll({ distinct: true, include: [N] }))
      .then(console.log)
      .catch(console.error);
    

    resolves to

    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: [] }
    

    but shouldn't there be an option to do

    sequelize.sync({ force: true })
      .then(() => M.findAndCountAll({ distinct: ['"M"."abc"', '"N"."xyz"'], include: [N] }))
      .then(console.log)
      .catch(console.error);
    

    which would resolve to

    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.

  9. marianyfs commented on Jul 3, 2019

    @marianyfs

    up

  10. added a commit that references this issue on Feb 20, 2020
    e35a142
  11. PokatilovArt commented on Jun 21, 2021

    @PokatilovArt

    I've setted these 2 properties

    distinct: true, col: "MODEL_NAME.MODEL_PRIMARY_KEY"

    and it's worked for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions