Skip to content

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

Closed

Description

@tsushanth

Description

An empty Op.notIn array (NOT IN ()) is logically always true. Sequelize correctly special-cases it at the top level, but the mechanism is "return an empty string," which is a valid identity element only for AND. Combined via Op.or or negated via Op.not, the empty fragment is silently dropped, changing the meaning of the whole clause and returning the wrong rows.

Steps to reproduce

// (age NOT IN ()) is always true, so OR should match every row:
await User.findAll({ where: { [Op.or]: [ { age: { [Op.notIn]: [] } }, { name: 'zzz-nonexistent' } ] } });
// observed: WHERE "name" = 'zzz-nonexistent'  -> 0 rows
// expected: all rows

Same root cause under Op.not: { [Op.not]: { age: { [Op.notIn]: [] } } } produces an empty WHERE => all rows, when NOT(always-true) should return none. (The AND case is correct; Op.in: [] under OR is fine -- it emits IN (NULL), the correct always-false identity for OR.) Reproduced against sqlite.

Root cause

packages/core/src/abstract-dialect/where-sql-builder.ts: the empty Op.notIn handler returns ''; joinWithLogicalOperator unconditionally filters empty fragments (filter(Boolean)), safe for AND but discarding an always-true operand under OR; and wrapWithNot('') returns '', collapsing NOT(always-true) to "all rows."

Suggested fix

Emit a real always-true boolean literal (1 = 1 / dialect TRUE) for empty Op.notIn instead of '', mirroring how empty Op.in emits the always-false IN (NULL). The top-level behavior (#4859 / #4860) is preserved, and OR/NOT composition becomes correct.

Activity

  1. added
    pending-approval
    on Jul 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pending-approval

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions