Repository navigation
Open
Open
Copy link
Labels
status: awaiting responseFor issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no actiontopic: countFor issues and PRs. Things that involve counting such as the `count` and the `findAndCountAll` meth.
Description
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
Metadata
Metadata
Assignees
Labels
status: awaiting responseFor issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no actiontopic: countFor issues and PRs. Things that involve counting such as the `count` and the `findAndCountAll` meth.
Activity
stale commentedon 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 🙂
dsbert commentedon Feb 19, 2019
Issue can be worked around with distinct and options.col #6114
stale commentedon 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 🙂
chjiyun commentedon Aug 4, 2019
i have this error today, Has this problem never been solved?
@aristocr2t @dsbert @jonmagic @mphilpot @aslakhellesoy
papb commentedon 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.