fix(postgres-adapter): keep table alias on UPDATE and DELETE - #30217
fix(postgres-adapter): keep table alias on UPDATE and DELETE#30217lazerg wants to merge 2 commits into
Conversation
Signed-off-by: Lazizbek Ergashev <lazerg2@gmail.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yml Review profile: CHILL Plan: Team Run ID: 📒 Files selected for processing (3)
Included review availability: Your plan provides up to 10 included reviews per hour; 4 remain after this review. 📝 WalkthroughWalkthroughPostgreSQL mutation construction and rendering now preserve explicit table aliases for UPDATE and DELETE statements. RETURNING projections also use the alias. Tests cover generated SQL and aliased returned-column references. ChangesPostgreSQL DML alias rendering
Estimated code review effort: 2 (Simple) | ~10 minutes Merge Risk: ⚪ Minimal · up to Aliased PostgreSQL UPDATE and DELETE statements now retain their target alias and use it in RETURNING projections, preventing invalid SQL references. The supplied coverage supports merge readiness. Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
Review of prisma#30217 found the sql-renderer fix alone was incomplete: the query builder's UpdateQueryImpl/DeleteQueryImpl always built RETURNING projections from the table's physical name, ignoring an alias set via .as(). An aliased UPDATE/DELETE built through the fluent builder (not just the raw AST path from the original issue) still produced RETURNING "table"."col" against a FROM target rendered as "table" AS "alias", which Postgres rejects the same way as the original bug. Adds a small returningTableRef() helper so RETURNING prefers the alias when present, and regression tests in builders.test.ts confirming it (verified fail-then-pass against the pre-fix code). Also tightens the adapter.test.ts regression test added for the original fix: realistic literal values instead of placeholder strings, and RETURNING added to the DELETE case so it exercises the same alias path as UPDATE. Signed-off-by: Lazizbek Ergashev <lazerg2@gmail.com>
@prisma/orm-extension-arktype-json
@prisma/orm-extension-middleware-cache
@prisma/orm-extension-paradedb
@prisma/orm-extension-pgvector
@prisma/orm-extension-postgis
@prisma/orm-extension-supabase
@prisma/orm-family-mongo
@prisma/orm-family-sql
@prisma/orm-framework
@prisma/orm-mongo
@prisma/orm-postgres
@prisma/orm-sqlite
@prisma/orm-target-mongo
@prisma/orm-target-postgres
@prisma/orm-target-sqlite
@prisma/orm-toolchain
commit: |
|
Closing this. The linked issue (#30211) got closed as not planned for the same reason as the rest of this batch: the reporter said these Codex-found reproductions might not hold up. |
Linked issue
Fixes #30211
Summary
The Postgres renderer qualified an UPDATE/DELETE target through
qualifyTableFromNamespaceCoordinatedirectly, which only resolves the bare table name and ignoresTableSource.alias. SELECT already went throughrenderTableSource, which appendsAS aliaswhen present, so an aliased UPDATE or DELETE rendered a WHERE/RETURNING clause referencing the alias against a FROM target that never declared it, and Postgres rejected the statement. Both statements now render throughrenderTableSourceso the target keeps its alias.Follow-up (self-review): the renderer-level fix alone didn't cover the query builder path.
UpdateQueryImpl/DeleteQueryImplinsql-builderalways built theirRETURNINGprojection from the table's physical name, not fromTableSource.alias. An aliasedUPDATE/DELETEbuilt via.as('p').update(...).returning(...)(not just the raw-AST path from the issue's repro) still producedRETURNING "table"."col"against aFROM "table" AS "p"target — the same class of Postgres error. Added a smallreturningTableRef()helper soRETURNINGprefers the alias when present, with regression tests inbuilders.test.ts(verified fail-then-pass).INSERTwas left unchanged in both passes: it never renders a target alias (itsON CONFLICT ... excludedsemantics don't need one), so there was nothing for itsRETURNINGto get wrong.Testing performed
pnpm --filter @internal/adapter-postgres test(added a case assertingUPDATE "post" AS "p" ...andDELETE FROM "post" AS "p" ...with realistic literals andRETURNINGon both statements; fails against the pre-fix code with the alias silently dropped, passes after)pnpm --filter @internal/sql-builder test(added builder-level regression tests assertingRETURNINGprojections use the alias, not the table name, for both UPDATE and DELETE; verified fail-then-pass)pnpm test:packages(full suite): 15715 passed, 3 expected fail, 3 skipped; one unrelatedpnpm pack/module-identity environment flake, reproduced identically on pristine mainpnpm typecheck && pnpm lintSkill update
n/a — internal only
Checklist
git commit -s)Summary by CodeRabbit
Bug Fixes
UPDATEandDELETEstatements now correctly preserve explicit table aliases in generated SQL.RETURNINGclauses now reference the correct table alias when one is provided.Tests
UPDATEandDELETEstatements, includingRETURNINGclauses.