Skip to content

fix: handle nested partial select with left join when first column is null#5436

Open
Iceshen87 wants to merge 1 commit intodrizzle-team:mainfrom
Iceshen87:fix/nested-select-leftjoin-null
Open

fix: handle nested partial select with left join when first column is null#5436
Iceshen87 wants to merge 1 commit intodrizzle-team:mainfrom
Iceshen87:fix/nested-select-leftjoin-null

Conversation

@Iceshen87
Copy link

Description

Fixes bug #1603 where nested partial select returns null for the entire object when the first column value is null in a left join.

Bug

When using .leftJoin() with nested object selection, if the first field in the nested object is null, the entire object returns null instead of containing the actual field values.

const org = await db.select({
  branding: {
    logo: orgBrandingTable.logo,           // null in DB
    panelBackground: orgBrandingTable.panelBackground,  // "#1a8cff"
  }
}).leftJoin(...)

// Before: { branding: null } ❌
// After: { branding: { logo: null, panelBackground: "#1a8cff" } } ✅

Root Cause

The nullifyMap logic in utils.ts was tracking object-level nullability based on the first field encountered. If the first field was null, it marked the entire object for nullification.

Solution

Changed nullifyMap to track individual field nullability:

  • New format: { tableName: string, hasNonNullValue: boolean }
  • Only nullify nested object if ALL fields are null AND the join is nullable
  • Preserves partial data when some fields have values

Testing

Related


/claim #1603 for Algora bounty

… null

- Fix bug where entire nested object becomes null if first field is null
- Track individual field nullability instead of object-level
- Only nullify nested object if ALL fields are null AND join is nullable
- Add comprehensive test coverage for edge cases

Fixes: drizzle-team#1603
@Iceshen87
Copy link
Author

Hi! Thanks for reviewing this PR. I noticed it doesn't have a bounty label. Does Drizzle ORM offer bounties for bug fixes? If so, could you please add the appropriate bounty label? Thanks!

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