Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
aristocr2t opened this issue Nov 7, 2018 · 5 comments
Labels
status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action topic: count For issues and PRs. Things that involve counting such as the `count` and the `findAndCountAll` meth. type: bug

Comments

@aristocr2t
Copy link

aristocr2t commented Nov 7, 2018

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

@stale
Copy link

stale bot commented Feb 17, 2019

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 🙂

@stale stale bot added the stale label Feb 17, 2019
@dsbert
Copy link

dsbert commented Feb 19, 2019

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

@stale stale bot removed the stale label Feb 19, 2019
@stale
Copy link

stale bot commented May 20, 2019

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 🙂

@stale stale bot added the stale label May 20, 2019
@stale stale bot closed this as completed May 27, 2019
@chjiyun
Copy link

chjiyun commented Aug 4, 2019

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

@papb papb reopened this Aug 5, 2019
@stale stale bot removed the stale label Aug 5, 2019
@papb papb added status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action type: bug labels Aug 5, 2019
@papb
Copy link
Member

papb commented Aug 5, 2019

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.

@papb papb added the topic: count For issues and PRs. Things that involve counting such as the `count` and the `findAndCountAll` meth. label Jan 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action topic: count For issues and PRs. Things that involve counting such as the `count` and the `findAndCountAll` meth. type: bug
Projects
None yet
Development

No branches or pull requests

4 participants