Skip to content

fix(core): make empty Op.notIn compose correctly under Op.or and Op.not - #18250

Merged
WikiRik merged 3 commits into
sequelize:mainfrom
gurusharankumarram1:fix/op-notin-empty-array
Jul 13, 2026
Merged

WikiRik merged 3 commits into
sequelize:mainfrom
gurusharankumarram1:fix/op-notin-empty-array

Conversation

@gurusharankumarram1

@gurusharankumarram1 gurusharankumarram1 commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor

Pull Request Checklist

  • Have you added new tests to prevent regressions?
  • If a documentation update is necessary, have you opened a PR to the documentation repository?
  • Did you update the typescript typings accordingly (if applicable)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Does the name of your PR follow our conventions?

Description of Changes

Closes #18248

An empty Op.notIn array is logically always true (NOT IN () matches every row), and Sequelize special-cased it by emitting an empty string. An empty fragment is a safe identity only for AND: both joinWithLogicalOperator (which filters out empty fragments) and wrapWithNot (which returns '' for empty input) silently drop it, changing the meaning of the query when it is composed:

// (age NOT IN ()) is always true, so OR should match every row:
User.findAll({ where: { [Op.or]: [{ age: { [Op.notIn]: [] } }, { name: 'zzz' }] } });
// before: WHERE `name` = 'zzz'          -> wrong rows
// after:  WHERE 1 = 1 OR `name` = 'zzz' -> all rows ✔

// NOT(always-true) should match no rows:
User.findAll({ where: { [Op.not]: { age: { [Op.notIn]: [] } } } });
// before: no WHERE clause    -> all rows
// after:  WHERE NOT (1 = 1)  -> no rows ✔

An empty Op.notIn now emits 1 = 1 — an always-true condition valid in every supported dialect — mirroring how an empty Op.in emits the always-false IN (NULL).

Added regression tests covering the emitted fragment itself and its composition under Op.or, Op.not and Op.and.

Note: a top-level { [Op.notIn]: [] } now generates WHERE 1 = 1 instead of omitting the WHERE clause entirely. This is semantically identical (all rows, preserving #4859 / #4860), but code asserting on the exact generated SQL will see a difference.

Credit: root cause analysis and suggested approach by @tsushanth in #18248.

List of Breaking Changes

None.

Summary by CodeRabbit

  • Bug Fixes

    • Fixed empty NOT IN filters to emit a valid always-true predicate instead of being omitted from the query.
    • Improved consistency when empty NOT IN appears inside AND, OR, and NOT combinations.
  • Tests

    • Updated SQL query expectations for empty NOT IN.
    • Added test coverage for nested empty NOT IN scenarios.

@gurusharankumarram1
gurusharankumarram1 requested a review from a team as a code owner July 8, 2026 12:40
@coderabbitai

coderabbitai Bot commented Jul 8, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f97da5aa-27e8-47c3-b7e2-e2d644d1768c

📥 Commits

Reviewing files that changed from the base of the PR and between 70fdb75 and 3010f9b.

📒 Files selected for processing (1)
  • packages/core/test/unit/sql/where.test.ts
💤 Files with no reviewable changes (1)
  • packages/core/test/unit/sql/where.test.ts

📝 Walkthrough

Walkthrough

Empty Op.notIn arrays now emit the always-true SQL literal 1 = 1 instead of an empty string. Tests cover standalone usage and composition under Op.or, Op.not, and Op.and.

Changes

Empty Op.notIn always-true fix

Layer / File(s) Summary
Op.notIn empty-array handling
packages/core/src/abstract-dialect/where-sql-builder.ts, packages/core/test/unit/sql/where.test.ts
Empty Op.notIn arrays now generate 1 = 1; tests verify standalone output and composition under Op.or, Op.not, and Op.and.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Suggested reviewers: sdepold, ephys

🚥 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 describes the empty Op.notIn composition fix under Op.or and Op.not.
Linked Issues check ✅ Passed The code and tests implement the always-true empty Op.notIn fix and cover OR, NOT, AND, and top-level behavior.
Out of Scope Changes check ✅ Passed The changes stay focused on empty Op.notIn handling and related regression tests.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@SippieCup SippieCup self-assigned this Jul 12, 2026
@SippieCup
SippieCup self-requested a review July 12, 2026 22:10

@SippieCup SippieCup left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

Simple and well tested for any regressions. no changes needed.

@SippieCup

Copy link
Copy Markdown
Contributor

@WikiRik this is good to merge.

I did think about one random "bug" just now though. index generation might be broken if this ever happens on mssql/psql since they don't like no-column comparisons. But I can't see anyone really building that into a index where statement, maybe if some programmatic one was created?

Anyway, I thought it should be noted and then just move on with life, and let the poor SOB who does have this happen have this comment here to tell them to fix their where statement.

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.

Op.notIn: [] produces logically-wrong SQL under Op.or and Op.not

3 participants