Skip to content

wrong working method findAndCountAll(), when have include models and distinct: true #10137

Open

Description

@aristocr2t

What are you doing?

when I use distinct: true property, I have an error: syntax error at or near "*", request: SELECT count(DISTINCT(*)) AS "count" <...>

SomeModel.addScope('someScope', (params) => {
  <...>
  return {
    attributes,
    order: [...order, [someInclude1, 'id', 'asc'], [someInclude2, 'id', 'asc']],
    where: { [Op.and]: where },
    offset: params.pageSize > -1 ? params.pageIndex * params.pageSize : 0,
    limit: params.pageSize > -1 ? params.pageSize : null,
    include: [
      someInclude1,
      someInclude2,
      { model: someModel3.scope('short'), as: 'someModel3' },
      { model: someModel4.scope('short'), as: 'someModel4' },
      { model: someModel5.scope('short'), as: 'someModel5' }
    ],
    distinct: true
  }
});
<...>
let result = await SomeModel.scope({ method: ['someScope', params] }).findAndCountAll();

I found error on the model.js file (https://github.com/sequelize/sequelize/blob/v4/lib/model.js#L1841):

  static count(options) {
    return Promise.try(() => {
      options = _.defaults(Utils.cloneDeep(options), { hooks: true });
      if (options.hooks) {
        return this.runHooks('beforeCount', options);
      }
    }).then(() => {
      let col = options.col || '*';
      if (options.include) { // <--- options.include is empty, but I dont know why
        col = this.name + '.' + (options.col || this.primaryKeyField);
      }
      console.log('count options:', options); // <- I log the options to find the problem

      options.plain = !options.group;
      options.dataType = new DataTypes.INTEGER();
      options.includeIgnoreAttributes = false;

      // No limit, offset or order for the options max be given to count()
      // Set them to null to prevent scopes setting those values
      options.limit = null;
      options.offset = null;
      options.order = null;

      return this.aggregate(col, 'count', options);
    });
  }

In the console I got next value: count options: { hooks: true }, as you can see there is no include value

Dialect: postgres
Sequelize version: 4.41.0

Activity

  1. stale commented on Feb 17, 2019

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

  2. dsbert commented on Feb 19, 2019

    @dsbert

    Issue can be worked around with distinct and options.col #6114

  3. stale commented on May 20, 2019

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

  4. chjiyun commented on Aug 4, 2019

    @chjiyun

    i have this error today, Has this problem never been solved?
    @aristocr2t @dsbert @jonmagic @mphilpot @aslakhellesoy

  5. reopened this on Aug 5, 2019
  6. added
    status: awaiting response
    type: bug
    on Aug 5, 2019
  7. papb commented on Aug 5, 2019

    @papb
    Member

    OP has posted a code snippet that shows the problem but is not entirely self-contained (i.e. I can't just copy-paste it and run it). Can someone please provide a SSCCE (also known as MCVE/reprex)? We need this to be able to tackle the bug properly.

  8. added
    topic: count
    on Jan 16, 2020
  9. added theissue type on Mar 9, 2025
  10. removed
    type: bug
    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

    status: awaiting responsetopic: count

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions