Commit 80f1dcd
fix(driver-sql): correlate PG introspectForeignKeys on the constraint and the key ordinal (#11396)
* fix(driver-sql): correlate PG introspectForeignKeys on the constraint and the key ordinal
Rewrites the Postgres arm of `SqlDriver.introspectForeignKeys` onto
`pg_constraint`, replacing a three-view `information_schema` join that was
wrong in two independent ways (both measured on live PostgreSQL 16.13):
1. `ccu.table_schema = tc.table_schema` demanded parent and child share a
schema. `constraint_column_usage` describes the REFERENCED side of a
foreign key, so its `table_schema` is the parent's — a cross-schema
target contributed zero rows and the table reported having no foreign
keys at all.
2. The kcu/ccu join carried no ordinal correlation, so an N-column key came
back as the N x N cartesian product (a 2-column key measured as 4 rows).
`constraint_column_usage` exposes no ordinal column, so defect 2 has nothing
to correlate on inside `information_schema`; correlating on
`tc.constraint_schema` (the spelling `introspectUniqueConstraints` carries)
was measured to fix defect 1 and leave defect 2 at 4 rows. `pg_constraint`
carries both facts on one row: `conkey`/`confkey` are parallel `smallint[]`s
in key order, so `unnest(...) WITH ORDINALITY` — the shape
`introspectPrimaryKeys` already uses for `indkey` — pairs child column with
parent column by construction and pins the key order.
`IntrospectedForeignKey`'s shape is unchanged; its doc comment now states
the ordered-sibling-rows contract the ORDER BY establishes.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RfyXxZ2WPjcjhuXpiQQc3y
* chore(driver-sql): changeset for the PG introspectForeignKeys join-correlation fix
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01RfyXxZ2WPjcjhuXpiQQc3y
---------
Co-authored-by: Claude <noreply@anthropic.com>1 parent 8d237b4 commit 80f1dcd
3 files changed
Lines changed: 488 additions & 33 deletions
File tree
- .changeset
- packages/drivers/driver-sql/src
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
0 commit comments