Skip to content

fix nested partial select returning null on left join#5808

Open
ThaiTrevor wants to merge 4 commits into
drizzle-team:mainfrom
ThaiTrevor:fix/issue-1603-drizzle-orm-bug-nested-partial-select
Open

fix nested partial select returning null on left join#5808
ThaiTrevor wants to merge 4 commits into
drizzle-team:mainfrom
ThaiTrevor:fix/issue-1603-drizzle-orm-bug-nested-partial-select

Conversation

@ThaiTrevor
Copy link
Copy Markdown

🤖 AI-assisted contribution — This PR was drafted with AI assistance and reviewed by a native Vietnamese speaker before submission. Placeholders, terminology, and file format were validated automatically. I will respond to review feedback. Happy to revise or close if not a fit.


Summary

Fixes nested partial select wrongly collapsing to null on a left join when only the first column in the nested object is null.

In mapResultRow (drizzle-orm/src/utils.ts), nullifyMap[objectName] is seeded from the first column of a nested object and is only updated later when a column comes from a different table. That meant: if the first column of a left-joined nested object happened to be null but the remaining columns from the same joined table had non-null values, the object was still nullified — losing real data.

The check now also clears the nullify marker when a same-table column has a non-null value, so the object is only nullified when all of its joined columns are null.

Changes

  • drizzle-orm/src/utils.ts — extend nullifyMap update to reset to false when a non-null value is seen from the same joined table.
  • drizzle-orm/tests/mapResultRow.test.ts — regression tests covering: first column null + others non-null (object kept), all columns null (object nullified), first column non-null + others null (object kept).

Closes #1603

Test plan

  • pnpm test tests/mapResultRow.test.ts — 3 new tests pass
  • pnpm test — 545 tests pass; 2 unrelated sqlite test files fail to load due to better-sqlite3 native binding not compilable in the sandbox (missing Visual Studio), no behaviour regression.

Closes drizzle-team#1188

mysql2 returns BINARY/VARBINARY columns as Node Buffer, but drizzle-orm
inferred them as `string` and lossily called `.toString()` on the Buffer
in `mapFromDriverValue`, corrupting arbitrary binary content (e.g. a
UUID stored as `binary(16)`).

Align the column data type with the driver:
- `dataType` is now `'buffer'` (same convention as `sqlite-core` blob in
  buffer mode).
- The inferred `data` type is `Buffer`; `driverParam` accepts
  `Buffer | string` since mysql2 also accepts hex strings.
- `mapFromDriverValue` returns the `Buffer` as-is (or wraps a
  `Uint8Array`/`string` via `Buffer.from`).

Updates the type-test default values and the mysql "all types"
integration test expectations to use `Buffer` accordingly.
@ThaiTrevor ThaiTrevor marked this pull request as ready for review May 26, 2026 02:43
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.

[BUG]: Nested Partial Select returns null on left join if first column value is null

1 participant