Skip to content

fix(orm): keep null on optional Boolean fields on MySQL and SQLite - #2924

Open
SibilSoren wants to merge 1 commit into
zenstackhq:devfrom
SibilSoren:fix/null-optional-boolean
Open

SibilSoren wants to merge 1 commit into
zenstackhq:devfrom
SibilSoren:fix/null-optional-boolean

Conversation

@SibilSoren

@SibilSoren SibilSoren commented Nov 17, 2026 •

Copy link
Copy Markdown

Fixes #2918

On MySQL and SQLite, transformInput turns a Boolean into 1 or 0 with value ? 1 : 0, so null becomes 0. Writing null to a Boolean? field stores false, as the issue describes. The issue mentions MySQL, but sqlite.ts has the same line.

Filters go through the same function, so { flag: { equals: null } } becomes flag = 0 and returns the false rows instead of the NULL ones. { flag: null } already worked, because it's turned into IS NULL before it gets there.

The fix keeps null as null in the Boolean case of both dialects, the same way the Decimal case a few lines below does. PostgreSQL passes booleans through as they are, so it wasn't affected.

Tests

New tests/regression/test/issue-2918.test.ts, which runs on whatever TEST_DB_PROVIDER is set:

  • writes null with create, update, createMany and updateMany, then checks that all four rows match flag: null and none match flag: false
  • equals: null finds only the row whose flag was never set, and not: { equals: null } counts the other two

On dev both tests fail on SQLite and MySQL 8.4 (expected { id: 1, flag: false } to match object { flag: null }) and pass on PostgreSQL 16. With the fix they pass on all three.

Locally:

  • tests/regression: 301 passed on SQLite and on MySQL 8.4
  • tests/e2e: 1127 passed on SQLite
  • the other packages' tests pass on both
  • lint and the regression tsc are clean

Thanks @aamarin for the clear report and for pointing at the right line.

Summary by CodeRabbit

  • Bug Fixes
    • Optional Boolean values set to null are now preserved when creating or updating records in MySQL and SQLite.
    • Filtering for null and false Boolean values now returns the expected results.
  • Tests
    • Added regression coverage for creating, updating, and filtering records with optional Boolean fields.

The MySQL and SQLite dialects turned every Boolean input into 1 or 0, so
null became 0. Writing null to a Boolean? field stored false, and an
equals: null filter matched the false rows instead of the null ones.
Keep null as null, like the Decimal case already does.

Fixes zenstackhq#2918
@coderabbitai

coderabbitai Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

MySQL and SQLite now preserve null Boolean inputs instead of converting them to zero. Regression tests cover create and update operations, omitted values, and null-related filters.

Changes: Nullable Boolean handling

Layer / File(s) Summary
Preserve null Boolean inputs
packages/orm/src/client/crud/dialects/mysql.ts, packages/orm/src/client/crud/dialects/sqlite.ts, tests/regression/test/issue-2918.test.ts
Both dialects preserve null Boolean inputs and retain the existing conversion for non-null values. Regression tests cover explicit null values in create and update operations, omitted values, and null-related filters.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: ymc9

Merge Risk: Minimal · up to dbe47

This change keeps null values on optional Boolean fields for MySQL and SQLite instead of storing them as false. The change is small and covered by new regression tests, so merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: optional Boolean fields keep null instead of being stored as 0 on MySQL and SQLite. The raw summary confirms the dialect changes in mysql.ts and sqlite.ts, and the obj…
Linked Issues check Passed Issue #2918 requires that writing null to an optional Boolean? field on MySQL stores NULL and reads back as null, for create and update. The issue names the cause: the Boolean case in MySqlCrudDialect…
Out of Scope Changes check Passed The PR changes two lines in the Boolean case of transformInput: one in mysql.ts (the issue's scope) and one in sqlite.ts. The sqlite.ts change applies the same fix to the same defect, which the PR des…
Full details: Title check

Explanation

The title names the main change: optional Boolean fields keep null instead of being stored as 0 on MySQL and SQLite. The raw summary confirms the dialect changes in mysql.ts and sqlite.ts, and the objectives confirm the same fix for issue #2918. The title is specific and matches the changeset.

Full details: Linked Issues check

Explanation

Issue #2918 requires that writing null to an optional Boolean? field on MySQL stores NULL and reads back as null, for create and update. The issue names the cause: the Boolean case in MySqlCrudDialect.transformInput returns value ? 1 : 0, which maps null to 0. The reviewed diff (mergeBase to head) changes that case to value !== null ? (value ? 1 : 0) : value, which matches the Decimal guard the issue suggests. Non-null values keep their 1/0 mapping. The PR also adds tests/regression/test/issue-2918.test.ts (per the summary, +42 lines) that cover create, update, createMany and updateMany with null, and equals: null filters. The regression tests were not run during this review. The test results come from the author's description and were not independently verified. The code change alone establishes the write-path behavior requested by the issue.

Full details: Out of Scope Changes check

Explanation

The PR changes two lines in the Boolean case of transformInput: one in mysql.ts (the issue's scope) and one in sqlite.ts. The sqlite.ts change applies the same fix to the same defect, which the PR description says exists there too. It is not an unrelated change. The equals: null filter behavior changes as a side effect, because filter values pass through the same transformInput function. The PR description and the regression test cover this case, so it is part of the same defect. The new regression test file is supporting test coverage for the objective. No unrelated changes were found in the reviewed diff for mysql.ts and sqlite.ts. The file list for the full head was truncated, so other files were not individually inspected. The summary lists only the three files above.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 0ea2711 and dbe47ba.

Files selected for processing: 3

  • packages/orm/src/client/crud/dialects/mysql.ts
  • packages/orm/src/client/crud/dialects/sqlite.ts
  • tests/regression/test/issue-2918.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

This branch has not been deployed

No deployments
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.

MySQL: null on an optional Boolean field is written as false

1 participant