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.

order in defaultScope merging rather than overwritten

See original GitHub issue

What are you doing?

Docs say “The limit, offset, order, paranoid, lock and raw fields are overwritten, while where is shallowly merged (meaning that identical keys will be overwritten). The merge strategy for include will be discussed later on.” but when i use order in defaultScope, and i use order in my query, order is merging not overwritten

when using this model

module.exports = (sequelize, DataTypes) => {
  const organizationMember = sequelize.define(
    "organizationMember",
    {
      id: {
        type: DataTypes.UUID,
        primaryKey: true,
        defaultValue: DataTypes.UUIDV4
      },
      organizationId: {
        type: DataTypes.UUID,
        allowNull: false
      },
      accountId: {
        type: DataTypes.UUID,
        allowNull: false
      },
      type: {
        type: DataTypes.STRING,
        allowNull: false
      },
      deletedAt: {
        type: DataTypes.DATE,
        allowNull: true
      }
    },
    {
      defaultScope: {
        order: [["createdAt", "ASC"]],
        where: { deletedAt: null }
      }
    }
  );
  return organizationMember;
};

then i query with

const organizationMember = await OrganizationMember.findAll({
  limit: 10,
  offset: 0,
  order: [["createdAt", "DESC"]],
  where: { organizationId: "97b5d956-c740-4100-ad1a-ea2a1364f9df" }
});

What do you expect to happen?

this sql query

SELECT "id", "organizationId", "accountId", "type", "deletedAt", "createdAt", "updatedAt" FROM "organizationMembers" AS "organizationMember" WHERE "organizationMember"."deletedAt" IS NULL AND "organizationMember"."organizationId" = '97b5d956-c740-4100-ad1a-ea2a1364f9df' ORDER BY "organizationMember"."createdAt" DESC LIMIT 10 OFFSET 0;

What is actually happening?

but i got this query

SELECT "id", "organizationId", "accountId", "type", "deletedAt", "createdAt", "updatedAt" FROM "organizationMembers" AS "organizationMember" WHERE "organizationMember"."deletedAt" IS NULL AND "organizationMember"."organizationId" = '97b5d956-c740-4100-ad1a-ea2a1364f9df' ORDER BY "organizationMember"."createdAt" ASC, "organizationMember"."createdAt" DESC LIMIT 10 OFFSET 0;

Environment

Dialect:

  • mysql
  • postgres
  • sqlite
  • mssql
  • any

Dialect library version: 7.10.0 Database version: 11.2 Sequelize version: 5.8.5 Node Version: 10.15.3 OS: Fedora 29

Tested with latest release:

  • No
  • Yes, specify that version: 5.8.5

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
toxuincommented, May 22, 2019

We are seeing the same behaviour on MySQL too. This is annoying because there’s no non-hacky way to fix it.

This behaviour does not make any sense: you specify the ordering you need results in, but the resulting SQL contains something completely different.

I believe the scope merging is doing a disservice in case of ordering. Docs are absolutely right: it should replace the existing order, not merge it in.

I was able to “fix” it by means of a dirty patch to sequelize, but I have no idea if it would break other things.

Please let me know if I am on the right path here:

in model.js in _injectScope function, I add a line:

if (options.order) scope.order = options.order;

right after cloning the this._scope.

Sequelize version 5.8.6, MySQL 5.7.24.

0reactions
DanielOverdevestcommented, Nov 4, 2022

Fixing this with this PR #15230

Read more comments on GitHub >

github_iconTop Results From Across the Web

Overriding a Rails default_scope - Stack Overflow
In my app, using PostgreSQL, the ordering in the default scope WINS. I'm removing all of my default_scopes and coding it in explicitly...
Read more >
Scopes - Sequelize
The limit , offset , order , paranoid , lock and raw fields are overwritten, while where is by default shallowly merged (meaning...
Read more >
scopes · Sequelize-docs - RYAN S CHATTERTON
The default scope can be removed by calling .unscoped() , .scope(null) , or by ... (rather than specifying the condition directly in that...
Read more >
#1812 default_scope can't take procs - Ruby on Rails - rails
Default scope isn't merged in until #scope is actually called which means that ... Specifically creation scopes (it uses a has_key? rather than...
Read more >
Using Named Scopes Across Models with ActiveRecord#Merge
I really want to see more people using this so please share this around! Want to learn more about Rails and become a...
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