diff --git a/drizzle-orm/src/utils.ts b/drizzle-orm/src/utils.ts index 6f7659485f..e7e5e4873e 100644 --- a/drizzle-orm/src/utils.ts +++ b/drizzle-orm/src/utils.ts @@ -51,6 +51,10 @@ export function mapResultRow( typeof nullifyMap[objectName] === 'string' && nullifyMap[objectName] !== getTableName(field.table) ) { nullifyMap[objectName] = false; + } else if (value !== null) { + // The initial value for an object may have been null, but subsquent values are not-null + // Prevents the entire object being define as null when the join has values + nullifyMap[objectName] = false; } } } diff --git a/integration-tests/tests/pg/pg-common.ts b/integration-tests/tests/pg/pg-common.ts index 311a731999..c7635eb5f0 100644 --- a/integration-tests/tests/pg/pg-common.ts +++ b/integration-tests/tests/pg/pg-common.ts @@ -1741,6 +1741,53 @@ export function tests() { ]); }); + test('left join (grouped join null first column)', async (ctx) => { + const { db } = ctx.pg; + + const rows = await db + .insert(citiesTable) + .values([{ name: 'Austin', state: "TX" }, { name: 'London' }]) + .returning({ id: citiesTable.id }); + + expect(rows).toHaveLength(2) + const [{id: austinId}, {id: londonId}] = rows as any as [{id: number}, {id: number}]; + + await db.insert(users2Table).values([{ name: 'John', cityId: austinId }, { name: 'Jane', cityId: londonId }]); + + const res = await db + .select( + { + id: users2Table.id, + user: { + name: users2Table.name, + nameUpper: sql`upper(${users2Table.name})`, + }, + city: { + // Being a nullable column, being first caused the entire object to return null + state: citiesTable.state, + id: citiesTable.id, + name: citiesTable.name, + nameUpper: sql`upper(${citiesTable.name})`, + }, + } + ) + .from(users2Table) + .leftJoin(citiesTable, eq(users2Table.cityId, citiesTable.id)); + + expect(res).toEqual([ + { + id: 1, + user: { name: 'John', nameUpper: 'JOHN' }, + city: { state: 'TX', id: austinId, name: 'Austin', nameUpper: 'AUSTIN' }, + }, + { + id: 2, + user: { name: 'Jane', nameUpper: 'JANE' }, + city: { state: null, id: londonId, name: 'London', nameUpper: 'LONDON' }, + }, + ]); + }); + test('select from a many subquery', async (ctx) => { const { db } = ctx.pg;