Skip to content

fix(core): query-level order takes precedence over scope order in _injectScope#18214

Open
Aviel212 wants to merge 2 commits into
sequelize:mainfrom
Aviel212:fix/scope-order-override-findall
Open

fix(core): query-level order takes precedence over scope order in _injectScope#18214
Aviel212 wants to merge 2 commits into
sequelize:mainfrom
Aviel212:fix/scope-order-override-findall

Conversation

@Aviel212
Copy link
Copy Markdown

@Aviel212 Aviel212 commented Apr 13, 2026

What

Fixes incorrect ordering when calling findAll (or findOne/findByPk) on a scoped model that has a predefined order.

Why

When a scoped model's order was merged into the query options via _injectScope, the internal _mergeFunction used union(scope.order, query.order) for all arrays — causing the scope's order to always be prepended ahead of the query-level order. This is a regression from Sequelize v3 behavior where the explicit query order took full precedence.

How

In _injectScope, if options.order is already set by the caller, we delete scope.order from the cloned scope before merging. The scope's order is still applied as a default when the query has no explicit order.

Tests

Added two unit tests to test/unit/model/scope.test.ts:

  1. Query-level order is not mixed with scope order when explicitly provided
  2. Scope order is still applied when the query has no order

All 55 existing scope unit tests continue to pass.

Closes #11415

Summary by CodeRabbit

  • Bug Fixes

    • Fixed an issue where default scope ordering could override or alter an explicitly provided query order; explicit query ordering is now preserved.
  • Tests

    • Added unit tests to verify that explicit query ordering remains unchanged and that scope defaults apply only when no explicit order is provided.

…jectScope

When a scoped model's findAll (or findOne/findByPk) is called with an
explicit `order` option, the scope's `order` was being prepended to it
due to the array union in _mergeFunction. This caused the scope order to
always win, which is incorrect and a regression from Sequelize v3.

Fix: if the query options already contain an `order`, delete the scope's
`order` before merging so that the query-level order is the only one used.

Closes sequelize#11415
@Aviel212 Aviel212 requested a review from a team as a code owner April 13, 2026 11:09
@Aviel212 Aviel212 requested review from ephys and sdepold April 13, 2026 11:09
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 13, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 56c13d2c-66f8-49a0-bc06-69ad8f0ba921

📥 Commits

Reviewing files that changed from the base of the PR and between d7f417d and 2b2f3ed.

📒 Files selected for processing (1)
  • packages/core/src/model.js
✅ Files skipped from review due to trivial changes (1)
  • packages/core/src/model.js

📝 Walkthrough

Walkthrough

Updated Model._injectScope(options) so that when caller-provided options.order is truthy, any order on the cloned scope is removed before merging, ensuring the query-level order remains authoritative; if options.order is absent, the scope's order is applied.

Changes

Cohort / File(s) Summary
Order Override Fix
packages/core/src/model.js
Modified _injectScope(options) to delete scope.order when options.order is explicitly provided, preventing scope-defined ordering from being prepended to or overriding query-level order.
Scope Order Tests
packages/core/test/unit/model/scope.test.ts
Added unit tests: one asserting an explicit options.order is preserved when injecting a scope, and another asserting the scope's order is applied when options.order is missing.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Suggested reviewers

  • sdepold

Poem

🐰
I nibbled through scope and query,
Found order fights where both would vie.
Now when you whisper "order, my way,"
The scope steps back and hops away.
Hooray — the query leads the sky!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main fix: ensuring query-level order takes precedence over scope order in _injectScope.
Linked Issues check ✅ Passed The PR fully addresses issue #11415 by implementing the exact expected behavior: query-level order now takes precedence over scope order, with scope order only applied when no query order is provided.
Out of Scope Changes check ✅ Passed All changes are directly related to fixing the scope order precedence issue; modifications to _injectScope and corresponding unit tests are within scope and necessary to resolve #11415.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 OpenGrep (1.17.0)
packages/core/src/model.js

┌──────────────┐
│ Opengrep CLI │
└──────────────┘

�[32m✔�[39m �[1mOpengrep OSS�[0m
�[32m✔�[39m Basic security coverage for first-party code vulnerabilities.

�[1m Loading rules from local config...�[0m


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect ordering for a findAll operation on a scoped model with predefined order

2 participants