Skip to content

Commit c1f6297

Browse files
authored
fix(db-postgres): joins with custom schema (#12937)
Fixes normal and polymorphic joins when using a custom schema in Postgres
1 parent c094b0e commit c1f6297

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

packages/drizzle/src/find/traverseFields.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ export const traverseFields = ({
513513
const subQueryAlias = `${columnName}_subquery`
514514

515515
let sqlWhere = eq(
516-
adapter.tables[currentTableName].id,
516+
sql.raw(`"${currentTableName}"."id"`),
517517
sql.raw(`"${subQueryAlias}"."${onPath}"`),
518518
)
519519

@@ -577,19 +577,23 @@ export const traverseFields = ({
577577

578578
let joinQueryWhere: Where
579579

580+
const currentIDRaw = sql.raw(
581+
`"${getNameFromDrizzleTable(currentIDColumn.table)}"."${currentIDColumn.name}"`,
582+
)
583+
580584
if (Array.isArray(field.targetField.relationTo)) {
581585
joinQueryWhere = {
582586
[field.on]: {
583587
equals: {
584588
relationTo: collectionSlug,
585-
value: rawConstraint(currentIDColumn),
589+
value: rawConstraint(currentIDRaw),
586590
},
587591
},
588592
}
589593
} else {
590594
joinQueryWhere = {
591595
[field.on]: {
592-
equals: rawConstraint(currentIDColumn),
596+
equals: rawConstraint(currentIDRaw),
593597
},
594598
}
595599
}

0 commit comments

Comments
 (0)