order in defaultScope merging rather than overwritten
See original GitHub issueWhat 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:
- Created 4 years ago
- Reactions:4
- Comments:8 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:right after cloning the
this._scope
.Sequelize version 5.8.6, MySQL 5.7.24.
Fixing this with this PR #15230