Skip to content

findAndCountAll does not work with distinct, count is right but rows is wrong #11468

Open

Description

@mkg20001

Issue Description

What are you doing?

// model

  class Delta extends Sequelize.Model {}
  Delta.init({
    id: {
      type: Sequelize.INTEGER,
      autoIncrement: true,
      primaryKey: true
    },

    padId: Sequelize.STRING,

    authorId: Sequelize.STRING,

    // delta: Sequelize.JSONB,
    delta: Sequelize.STRING(10000),
    deltaId: Sequelize.INTEGER
  }, { sequelize, modelName: 'delta' })

// query

let perPage = 25
let offset = 0

const result = await Delta.findAndCountAll({
  limit: perPage,
  offset,
  order: [
    ['padId', 'ASC']
  ],
  distinct: true,
  col: 'delta.padId'
})

What do you expect to happen?

It should count the distinct padIds and result.rows should be an array containing all the padId's

What is actually happening?

The count is working, the select just selects all the parameters instead of just the distinct padIds

Executing (default): SELECT count(DISTINCT("delta"."padId")) AS "count" FROM "delta" AS "delta";
Executing (default): SELECT "id", "padId", "authorId", "delta", "deltaId", "createdAt", "updatedAt" FROM "delta" AS "delta" ORDER BY "delta"."padId" ASC LIMIT 25 OFFSET 0;

Additional context

Add any other context or screenshots about the feature request here.

Environment

  • Sequelize version: [email protected]
  • Node.js version: v10.16.3
  • Operating System: Ubuntu 19.04 amd64
  • If TypeScript related: TypeScript version: none

Issue Template Checklist

How does this problem relate to dialects?

  • I think this problem happens regardless of the dialect.
  • I think this problem happens only for the following dialect(s):
  • I don't know, I was using PUT-YOUR-DIALECT-HERE, with connector library version XXX and database version XXX

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don't know how to start, I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.

Activity

  1. EricPKerr commented on Sep 25, 2019

    @EricPKerr

    I think I have a fix for this, but I'm not quite sure how to pull it into this project:

    In abstract/query-generator.js

    Change:

    // Add LIMIT, OFFSET to sub or main query
    const limitOrder = this.addLimitAndOffset(options, mainTable.model);
    if (limitOrder && !options.groupedLimit) {
      if (subQuery) {
        subQueryItems.push(limitOrder);
      } else {
        mainQueryItems.push(limitOrder);
      }
    }
    

    To this:

    // Add LIMIT, OFFSET to sub or main query
    const limitOrder = this.addLimitAndOffset(options, mainTable.model);
    if (limitOrder && !options.groupedLimit) {
      const noRequiredIncludes = _.isEmpty(options.include) || _.every(options.include, include => !include.hasRequired);
      if (subQuery && noRequiredIncludes) {
        subQueryItems.push(limitOrder);
      } else {
        mainQueryItems.push(limitOrder);
      }
    }
    

    Basically you don't want the LIMIT/OFFSET part to be in the subQuery if there are any joined (included) tables that are required: true and would potentially restrict the results. You need to move these to the mainQueryItems outer query.

    Only thing I'm not sure about is what hasParentRequired means in the options, and if this should actually be:

    const noRequiredIncludes = _.isEmpty(options.include) || _.every(options.include, include => !include.hasRequired && !include.hasParentRequired);
    
  2. added
    type: bug
    on Oct 23, 2019
  3. BartusZak commented on Dec 2, 2019

    @BartusZak

    Any update on that?

  4. SuperManifolds commented on Dec 20, 2019

    @SuperManifolds

    Bump

  5. papb commented on Jan 16, 2020

    @papb
    Member

    Related: #9481, #10137 and #10557,

  6. added
    topic: count
    on Jan 16, 2020
  7. RomeDorado commented on Mar 30, 2020

    @RomeDorado

    Still an issue.
    Version 5.21.5

  8. yongching commented on Jun 13, 2020

    @yongching

    Bump

  9. vndevil commented on Jun 16, 2020

    @vndevil

    Bump

  10. hiitsgabe commented on Aug 13, 2020

    @hiitsgabe

    Bump

  11. ori-osherov-vonage commented on Nov 17, 2020

    @ori-osherov-vonage

    Bump

  12. hiitsgabe commented on Nov 24, 2020

    @hiitsgabe

    I am using
    attributes: [[Sequelize.fn('DISTINCT', Sequelize.col('user_email')), 'user_email']] as a workaround to the problem. Works for me.

  13. mihai10001 commented on Mar 5, 2021

    @mihai10001

    Bump

  14. Adam-Burke commented on Apr 8, 2021

    @Adam-Burke

    help

  15. PokatilovArt commented on Jun 21, 2021

    @PokatilovArt

    #2713 (comment)

    worked for me

  16. O-mkar commented on Nov 2, 2021

    @O-mkar

    Same for me, Help

    Still an issue.
    Version 6.6.2

  17. dormeiri commented on Mar 16, 2022

    @dormeiri

    This what worked for me:

    attributes: [[fn('DISTINCT', col('myColumn')), 'myColumn']],
    col: 'myColumn',
    distinct: true

    Seems like attributes property is for the SELECT and the col/distinct properties are for the COUNT

  18. added theissue type on Mar 9, 2025
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

    topic: counttype: bug

    Type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions