drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too šŸ˜…
https://orm.drizzle.team
Apache License 2.0
24.46k stars 642 forks source link

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

Open oscarhermoso opened 11 months ago

oscarhermoso commented 11 months ago

What version of drizzle-orm are you using?

0.29.1

What version of drizzle-kit are you using?

0.19.12

Describe the Bug

The output of this PostgreSQL select query has a null value for the branding property. However, there was a successful join in the database.

Furthermore, if the order of the panelBackground and logo properties are swapped, the query works as expected.

const org = await db
  .select({
    name: orgTable.name,
    slug: orgTable.slug,
    branding: {
      logo: orgBrandingTable.logo,  // null in database
      panelBackground: orgBrandingTable.panelBackground,  // "#1a8cff" in database
    }
  })
  .from(orgTable)
  .leftJoin(orgBrandingTable, eq(orgTable.id, orgBrandingTable.orgId))
  .where(
    eq(orgTable.id, session.orgId),
  )

console.log(org);
// { name: 'Test org 2', slug: 'test-org-2', branding: null }

Expected behavior

After swapping panelBackground and logo in the query, this is the (correct) value of org

console.log(org);
// {
//   name: 'Test org 2',
//   slug: 'test-org-2',
//   branding: { panelBackground: '#1a8cff', logo: null   }
// }

Environment & setup

It's not a problem with the generated query, because when I execute in pgAdmin, it returns results.

select "org"."name", "org"."slug", "org_branding"."logo",  "org_branding"."panel_background_colour"
from "org"
left join "org_branding"
  on "org"."id" = "org_branding"."org_id"
where ("org"."id" = 11)

-- "name","slug","logo","panel_background_colour"
-- "Test org 2","test-org-2",NULL,"#1a8cff"

Versions

john-griffin commented 9 months ago

We also just hit this on drizzle-orm 0.29.3 and drizzle-kit 0.20.13.

algora-pbc commented 9 months ago

šŸ’Ž $50 bounty created by john-griffin šŸ‘‰ To claim this bounty, submit your pull request on Algora šŸ“ Before proceeding, please make sure you can receive payouts in your country šŸ’µ Payment arrives in your account 2-5 days after the bounty is rewarded šŸ’Æ You keep 100% of the bounty award šŸ™ Thank you for contributing to drizzle-team/drizzle-orm!

šŸ‘‰ Add a bounty ā€¢ Share on socials

john-griffin commented 9 months ago

CleanShot 2024-01-22 at 15 09 03@2x

codingarchitect-wq commented 9 months ago

/attempt #1603

Moocar commented 9 months ago

We just hit this bug in 0.28.6. I tried upgrading to version 0.29.3 but to no avail. Our work-around for now is to only load fields at the top-level. And then manually map over the results to convert them into the nested structure.

xriter commented 8 months ago

Also just hit this. Still present in 0.29.4 ā˜¹ļø