prisma icon indicating copy to clipboard operation
prisma copied to clipboard

fix(p2002): correct modelName in nested create unique constraint errors #29595

Open AnupamKumar-1 opened this issue 5 months ago • 2 comments

Fixes #29595

Problem

When a nested create caused a unique constraint violation (P2002), error.meta.modelName always reported the top-level model (e.g. AppVersion) instead of the model where the violation actually occurred (e.g. AppMajorVersion).

Root cause

The adapters were stripping the table name when parsing DB errors. By the time the error reached RequestHandler.ts, there was no model info left, so it just stamped the top-level operation's model name onto every P2002.

Fix

  • Added table? to UniqueConstraintViolation in driver-adapter-utils
  • Extract table name in better-sqlite3 and libsql from the SQLite error message
  • Use native error.table in pg and neon (already provided by the driver)
  • Set meta.modelName from table in user-facing-error.ts before RequestHandler can overwrite it
  • RequestHandler.ts now prefers error.meta.modelName if already set

Test

Added functional regression test in packages/client/tests/functional/issues/29595-p2002-nested-create-modelname/


AnupamKumar-1 avatar Jun 08 '26 11:06 AnupamKumar-1

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Jun 08 '26 11:06 CLAassistant

Review Change Stack

[!NOTE]

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • [ ] ▶️ Resume reviews
  • [ ] 🔍 Trigger review

Walkthrough

This PR adds table information to unique constraint violations across database adapters, propagates it through user-facing errors, and resolves the corresponding runtime model name in client error metadata. It also adds a provider-aware regression test for nested creates that verifies a duplicate AppMajorVersion record produces P2002 metadata with modelName set to AppMajorVersion.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main fix: nested-create P2002 errors now report the correct modelName.
Description check ✅ Passed The description matches the changeset and explains the bug, fix, and test coverage.
Linked Issues check ✅ Passed The changes preserve table info through adapters and map it back to the nested modelName as required by #29595.
Out of Scope Changes check ✅ Passed The adapter, runtime, and test updates all support the same nested-create P2002 modelName fix and do not appear unrelated.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • [ ] Create PR with unit tests
✨ Simplify code
  • [ ] Create PR with simplified code

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

coderabbitai[bot] avatar Jun 08 '26 11:06 coderabbitai[bot]

Hi @aqrln , I’ve addressed the latest CI failures and pushed the fixes. When you have time, could you please take another look? Thank you!

AnupamKumar-1 avatar Jul 22 '26 15:07 AnupamKumar-1

Resolved the blocking review question about `meta.modelName` and `@@map`.

The table→model mapping is done in `RequestHandler.resolveErrorMeta`/`modelNameForTable`: the driver adapters surface the violated table name on the `UniqueConstraintViolation` `MappedError` (new `table?` field), `user-facing-error.ts` forwards it as `meta.table`, and the client maps it back to the Prisma model via `_runtimeDataModel.models` (comparing `model.dbName ?? name`, i.e. the `@@map` target, against the reported table, unqualified). The lookup returns no match on ambiguity (two models sharing a bare table name under `@@schema`), falling back to the top-level operation's model name. The added functional test (`AppMajorVersion` mapped to `app_major_versions`) asserts `meta.modelName === 'AppMajorVersion'`.

Also merged `main` and reconciled with #29587: adapter-pg keeps the constraint index name for `meta.target` while still deriving the table for the modelName mapping. The mssql 2627 constraint target is left unchanged, so existing sqlserver snapshots are unaffected; table extraction there is purely additive.

Verified locally: `@prisma/client-engine-runtime` (217), adapter unit tests for pg/mssql/libsql/mariadb/neon/planetscale/d1 all pass; prettier clean. The functional suite was not run locally (the harness requires `db push --force-reset`).

tensordreams avatar Jul 23 '26 22:07 tensordreams

Hi @aqrln , @tensordreams ! I synced to the latest branch and ran the prisma-dot-dmmf (provider=postgresql, js_pg) test locally. It passes, and the snapshot is already up to date (no changes with -u). If possible, could you rerun the failed jobs? I also noticed the Lint failure doesn’t seem related to the changes in this PR. Thanks!

AnupamKumar-1 avatar Jul 24 '26 23:07 AnupamKumar-1