Skip to content

Is distinct option for Model.count() actually working? #4840

Closed

Description

@JakubJagoda

I have a Postgres database. In latest docs, distinct is listed under options that can be passed to Model.count() function. However, as col is explicitely set to '*' in this function implementation, resulting query looks as follows
SELECT COUNT(DISTINCT(*)) ...
which doesn't seem to be a valid query:

Executing (default): SELECT count(DISTINCT(*)) AS "count" FROM "Comments" AS "Comment";
Wed Nov 11 2015 21:23:23 GMT+0100 (CET) 'SequelizeDatabaseError' 'syntax error at or near "*"'
'SequelizeDatabaseError: syntax error at or near "*"
    at Query.formatError (/node_modules/sequelize/lib/dialects/postgres/query.js:443:14)
    at null.<anonymous> (/node_modules/sequelize/lib/dialects/postgres/query.js:118:19)
    at emitOne (events.js:77:13)
    at emit (events.js:169:7)
    at Query.handleError (/node_modules/pg/lib/query.js:108:8)
    at null.<anonymous> (/node_modules/pg/lib/client.js:171:26)
    at emitOne (events.js:77:13)
    at emit (events.js:169:7)
    at Socket.<anonymous> (/node_modules/pg/lib/connection.js:109:12)
    at emitOne (events.js:77:13)
    at Socket.emit (events.js:169:7)
    at readableAddChunk (_stream_readable.js:146:16)
    at Socket.Readable.push (_stream_readable.js:110:10)
    at TCP.onread (net.js:523:20)'

Sample code resulting in the error:

var Comment = sequelize.define('Comment', {
        author: Sequelize.STRING,
        text: Sequelize.TEXT
    });

var uniqueAuthors = Comment.count({
        distinct: true
    });

I tried also passing where or attributes hoping that this will change the resulting column, but the result is still the same. So, what is the proper way to use this option?

Activity

  1. mickhansen commented on Nov 12, 2015

    @mickhansen
    Contributor

    It's most likely not working looking at the code. The option description was likely copied from the aggregate method which count relies on.

  2. JakubJagoda commented on Nov 12, 2015

    @JakubJagoda
    Author

    Does it mean the bug is in the implementation or rather in the documentation (and shouldn't be there)?

  3. janmeier commented on Nov 18, 2015

    @janmeier
    Member

    Its used in connection with includes, where the field default to id

    return Post.count({ distinct: true, include: [{ model: PostComment, required: false }] }).then(function(count2) {

    Perhaps count should allow the user to set .col, that would make the distinct option useful

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions