question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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: sequelize@5.19.0
  • 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.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:4
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
SuperManifoldscommented, Dec 20, 2019

Bump

5reactions
RomeDoradocommented, Mar 30, 2020

Still an issue. Version 5.21.5

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get a distinct count with sequelize? - Stack Overflow
count method in lib/model.js, and tracing some code, I found that when using Model.count , you can just add any kind of aggregate...
Read more >
Aggregation, grouping, and summarizing (Concepts) - Prisma
Aggregation, grouping, and summarizing. Prisma Client allows you to count records, aggregate number fields, and select distinct field values.
Read more >
COUNT function - Amazon Redshift
The COUNT function counts the rows defined by the expression. ... For smaller sets of distinct values, in the thousands, approximation might be...
Read more >
Model Querying - Basics - Sequelize
Transactions are important to ensure data integrity and to provide ... model to set only an username but not an admin flag (i.e.,...
Read more >
Sequelize - how to COUNT table rows number with code ...
Sequelize Model.count() method is used to generate and execute a SELECT COUNT SQL query to your connected database.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found