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 with scope having attributes

See original GitHub issue

When having a defaultScope defined, which contains the attributes options, the count query is messed up.

defaultScope: {
        where: {
          active: true
        },
        attributes: ['id', 'username', 'email', 'active', 'approved', 'company_id']
      }

The following query is generated

SELECT "id", "username", "email", "active", "approved", "company_id", count(*) AS "count" FROM "users" AS "users" WHERE "users"."company_id" = 1 AND "users"."active" = true;

2 errors within the query. The first one, in that case, the “group by” is required. The second error is that it should normally not include the “attributes” for the count query. If I fix the first one (add the “group” option), the query runs, but “res.count” is actually returning anarray of objects from the query, with the “count” field inside.

Example :

{ count:
   [ { id: 1,
       username: 'Test',
       email: 'test@test,cin',
       active: true,
       approved: true,
       company_id: 1,
       count: '1' },
     { id: 2,
       username: 'Test2',
       email: 'test@test.com',
       active: true,
       approved: true,
       company_id: 1,
       count: '1' } ],
      rows:....
}

The error come from the file https://github.com/sequelize/sequelize/blob/master/lib/model.js

The findAndCountAll strips correctly the attributes options (https://github.com/sequelize/sequelize/blob/master/lib/model.js#L1604) But then within the count query, the scope is injected with the attributes and all other options that should be omitted (https://github.com/sequelize/sequelize/blob/master/lib/model.js#L1542)

I’d be happy to fix it, but I do not know what would be the best possible option to fix it. I see the following possibilities :

  • Add a parameter to the count function to omit the attributes/… options after the scope is injected
  • Separate the count function in 2, one which will prepare the parameters for the normal count, and the second that actually does the count. This way the findAndCount function would use directly the “count” part, as the option is already ready.

Any toughs on this ?

Thanks

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

6reactions
jay-depotcommented, Feb 25, 2017

@mickhansen It seems like the regression mentioned here is alive and well. This is a HUGE problem if your query uses attributes to create virtual columns, something like SELECT some_Function_Of("table"."column") AS property WHERE property = 'foo'; since during the COUNT step, the query bombs out on the error that column property doesn’t exist. Which it would, if we weren’t stripping off attributes indiscriminately here.

0reactions
mickhansencommented, Jan 12, 2016

@krisr That’s THE regression as far as i can tell 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sequelize v5 findAndCountAll Include Columns - Stack Overflow
There is no need to include attributes in count query because it's used for counting the total number of records that satisfy all...
Read more >
Scopes - Sequelize
Scopes are used to help you reuse code. You can define commonly used queries, specifying options such as where, include, limit, etc.
Read more >
Scopes - Manual | Sequelize
Scopes can include all the same attributes as regular finders, ... Sequelize has two different but related scope concepts in relation to associations....
Read more >
The Comprehensive Sequelize Cheatsheet
findByPk; findOne; findOrCreate; findAll; findAndCountAll; count ... To set up a basic model with only attributes and their datatypes.
Read more >
Sequelize Aggregate Functions (MIN, MAX, SUM, COUNT, etc ...
attributes : [[sequelize.fn('min', sequelize.col('price')), 'minPrice']],. }); ...
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