fix(orm): keep nested left-join object when a later column is set - #6152
Open
adsqx wants to merge 1 commit into
Open
fix(orm): keep nested left-join object when a later column is set#6152adsqx wants to merge 1 commit into
adsqx wants to merge 1 commit into
Conversation
… null `mapResultRow` tracks nested left-join objects in `nullifyMap`, where a table name marks the object as a candidate for nullification. Only a column from a different table cleared that marker, so a later non-null value from the same table left it in place and the whole nested object was wiped — making the result depend on the order of the nested keys. Also clear the marker when a later column of the same table is not null. A missing left-join row still nullifies the object, since all of its columns are null and the marker is never cleared. Closes drizzle-team#1603 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
/claim #1603
Fixes #1603
Fix nested partial select returning
nullon left join when the first column isnullCloses #1603
Problem
With a nested partial select over a
leftJoin, the whole nested object is mapped tonullwhenever the first selected column of that object happens to benull— even though the joined row exists and its other columns have values.Returns
branding: null. Swapping the two keys insidebrandingreturns{ panelBackground: '#1a8cff', logo: null }. The generated SQL is correct in both cases — the result is order-dependent purely because of the row mapper.Cause
In
mapResultRow(drizzle-orm/src/utils.ts), nested objects are tracked innullifyMap, where a table name means "every field seen so far for this object came from that table and wasnull" andfalsemeans "do not nullify".The first column of a nested object seeds the entry:
The
else iffor the following columns only cleared the entry when a column came from a different table. A later non-nullvalue from the same table left the table name in place, so after the reduce the object was wiped for any nullable join.Fix
One condition, keeping the existing
string | falseencoding: also clear the entry when a later column of the same table is notnull.A missing left-join row still nullifies the nested object, because in that case every column is
null, so the entry is never cleared and the existingjoinsNotNullableMapcheck at the end applies unchanged.This is the same code change as #6018; this PR adds the cases its test does not cover.
Tests
New
drizzle-orm/tests/map-result-row.test.tscallsmapResultRowdirectly (no database) and covers:null, a later one notnull→ object kept,null, laternull→ object kept (regression),null) → nested object isnull,nullon a not nullable join → object kept,nullone is nullified,joinsNotNullableMap→ nothing is nullified.Three of them fail on
mainand pass with the fix; the rest guard the behaviour that must not change.mapResultRowis shared by every driver, so the fix applies to all dialects.