GeezerVison is showing github.com — fix(adapters, client-engine-runtime): map check constraint violations to P2004 by ravitejas-tech · Pull Request #31227 · prisma/orm
Conversation
ravitejas-tech
commented
Nov 27, 2026
•
edited by coderabbitai
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
Fixes #31215
A check constraint violation came back as P2039 ("Database error. Code: ...") because no adapter recognised it. This adds a CheckConstraintViolation error kind and maps it to P2004, the documented code for a failed constraint. The message follows the other constraint errors: Check constraint failed on the constraint: `t_sum_check` .
Where I looked and what changed
- Postgres (
adapter-pg, adapter-neon, adapter-ppg): SQLSTATE 23514, with the name from the error's constraint field.
- MySQL / MariaDB (
adapter-mariadb): 3819 (MySQL, Check constraint 'x' is violated.) and 4025 (MariaDB, CONSTRAINT `x` failed for ...). PlanetScale: 3819.
- SQLite (
adapter-better-sqlite3, adapter-libsql): SQLITE_CONSTRAINT_CHECK / 275. SQLite gives the constraint name, or the expression for an unnamed constraint.
- D1:
CHECK constraint failed was reported as a foreign key violation (P2003). It now reports P2004.
- SQL Server: error
547 covers both CHECK and FOREIGN KEY conflicts, and was always reported as a foreign key violation. It now checks the message and reports CHECK conflicts as P2004.
Behaviour change to note: on D1 and SQL Server, check violations move from P2003 to P2004. Foreign key conflicts on both still return P2003, with a test for each.
Prisma 8 on main maps driver errors separately, so this only targets v7.
Testing
- Added unit tests for every adapter (new test files for
adapter-planetscale and adapter-d1) and for the P2004 mapping in client-engine-runtime. The new tests fail without the fix.
- Ran
pnpm vitest on the adapter, client-engine-runtime and driver-adapter-utils projects. Everything passes apart from 5 tests in better-sqlite3.test.ts that need the native module, which I couldn't build locally on Node 26.
- Took the Postgres error fields from a real Postgres 18 and the SQLite code and message from libSQL. The MySQL, MariaDB and SQL Server cases use their documented error messages.
All commits are signed off (DCO).
Summary by CodeRabbit
- Bug Fixes
- Check constraint violations are now consistently recognized across supported database adapters, with constraint names included when available.
- These errors now surface as
P2004 with a clear, constraint-specific message.
- Foreign key violations continue to be reported as foreign key errors, including when database messages distinguish them from check constraint failures.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
… to P2004
No driver adapter recognised a check constraint violation, so it fell
back to P2039 with the raw database error. D1 reported it as a foreign
key violation, and SQL Server reported its error 547 as one too, since
that number covers both CHECK and FOREIGN KEY conflicts.
Add a CheckConstraintViolation error kind, map it to P2004, and map
check violations to it in every adapter: Postgres 23514 (pg, neon, ppg),
MySQL 3819 (mariadb, planetscale), MariaDB 4025, SQLite
SQLITE_CONSTRAINT_CHECK / 275 (better-sqlite3, libsql), D1 and SQL Server.
Fixes prisma#31215
Signed-off-by: Raviteja Salva <[email protected]>
coderabbitai
Bot
commented
Nov 27, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|

No actionable comments were generated in the recent review.
Walkthrough
-
The adapter error type now includes CheckConstraintViolation. Database adapters map supported check-constraint errors to that type, and the client runtime reports it as P2004 with a constraint-specific message.
Changes: Check constraint error mapping
| Layer / File(s) |
Summary |
Mapped error contract packages/driver-adapter-utils/src/types.ts, AGENTS.md |
MappedError includes CheckConstraintViolation with an optional constraint index. The documented known error kinds include this variant. |
SQLite adapter mappings packages/adapter-better-sqlite3/src/errors.ts, packages/adapter-better-sqlite3/src/errors.test.ts, packages/adapter-d1/src/errors.ts, packages/adapter-d1/src/errors.test.ts, packages/adapter-libsql/src/errors.ts, packages/adapter-libsql/src/errors.test.ts |
These adapters map check-constraint errors to CheckConstraintViolation. Tests cover constraint details and preservation of the original message or code. |
MySQL adapter mappings packages/adapter-mariadb/src/errors.ts, packages/adapter-mariadb/src/errors.test.ts, packages/adapter-planetscale/src/errors.ts, packages/adapter-planetscale/src/errors.test.ts |
MariaDB and PlanetScale map check-constraint errors and extract constraint names from driver messages. Tests check the mapped fields. |
SQL Server constraint mapping packages/adapter-mssql/src/errors.ts, packages/adapter-mssql/src/errors.test.ts |
Error 547 maps CHECK conflicts to CheckConstraintViolation. FOREIGN KEY conflicts remain ForeignKeyConstraintViolation. |
PostgreSQL adapter mappings packages/adapter-neon/src/errors.ts, packages/adapter-neon/src/__tests__/errors.test.ts, packages/adapter-pg/src/errors.ts, packages/adapter-pg/src/__tests__/errors.test.ts, packages/adapter-ppg/src/errors.ts, packages/adapter-ppg/src/errors.test.ts |
These adapters map error 23514 to CheckConstraintViolation and include the constraint name when present. Tests check the mapped error fields. |
Client error rendering packages/client-engine-runtime/src/user-facing-error.ts, packages/client-engine-runtime/src/user-facing-error.test.ts |
The runtime maps CheckConstraintViolation to P2004 and renders a constraint-specific message. A test checks the error code, message, and metadata. |
Estimated code review effort: 3 (Moderate) | ~25 minutes
Change: Bug fix · Severity of issue fixed: Medium
Merge Risk: Minimal · up to dc6d3
-
Check-constraint violations now report as P2004 with a constraint-specific message across the supported database adapters. No merge-blocking risk remains.
Pre-merge checks: 4
-
| Check name |
Status |
Explanation |
| Description Check |
 |
Check skipped - CodeRabbit’s high-level summary is enabled. |
| Title check |
 |
The title names the main change: check constraint violations now map to P2004 across adapters and client-engine-runtime. The raw summary confirms the adapter mappings for Postgres, MySQL/MariaDB, Plan… |
| Linked Issues check |
 |
Issue #31215 requires check-constraint violations to map to P2004 instead of the P2039 fallback. The reviewed head adds a CheckConstraintViolation kind and maps native codes per adapter: PostgreSQL 23… |
| Out of Scope Changes check |
 |
Every changed file relates to the check-constraint objective. The changes add the CheckConstraintViolation kind in driver-adapter-utils types, map it in each adapter, map it to P2004 and render it in … |
Full details: Title check
-
Explanation
The title names the main change: check constraint violations now map to P2004 across adapters and client-engine-runtime. The raw summary confirms the adapter mappings for Postgres, MySQL/MariaDB, PlanetScale, SQLite/libSQL, D1, and SQL Server, plus the P2004 mapping in user-facing-error.ts. The title is specific and matches the objectives. It is 78 characters, which is slightly long but acceptable.
Full details: Linked Issues check
-
Explanation
Issue #31215 requires check-constraint violations to map to P2004 instead of the P2039 fallback. The reviewed head adds a CheckConstraintViolation kind and maps native codes per adapter: PostgreSQL 23514 (adapter-pg, adapter-neon, adapter-ppg), MySQL 3819 and MariaDB 4025 (adapter-mariadb), PlanetScale 3819 (adapter-planetscale), SQLite 275 (adapter-better-sqlite3, adapter-libsql), D1 CHECK errors (adapter-d1), and SQL Server 547 when the message contains CHECK (adapter-mssql). client-engine-runtime maps the kind to P2004 and renders the constraint name. Unit tests cover each adapter and the P2004 mapping. The incremental change since the previous review adds a libsql fallback. It maps a CHECK constraint message to CheckConstraintViolation when the error has no numeric code, which covers remote connections. Foreign-key conflicts stay P2003 on D1 and SQL Server. Five better-sqlite3 tests need a native module and were not run. This is an environment limit and does not affect the mapping evidence.
Full details: Out of Scope Changes check
-
Explanation
Every changed file relates to the check-constraint objective. The changes add the CheckConstraintViolation kind in driver-adapter-utils types, map it in each adapter, map it to P2004 and render it in client-engine-runtime, and add matching tests. The AGENTS.md change documents the new MappedError kind. The incremental change touches only adapter-libsql code and its test, and that code serves the same objective. The D1 and SQL Server move from P2003 to P2004 for check violations is part of the objective and is stated in the PR description. No unrelated change appears in the reviewed evidence.
Finishing Touches
-
Generate unit tests (beta)
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info
-
Run configuration
-
- Source mode: Auto
- Repository: prisma/orm
- Branch: v7
- File: .coderabbit.yml
- Commit: 4c3db4a
- Layers: Repository YAML
- Review profile: ASSERTIVE
- Plan: Advanced
- Run ID:
54453ed8-b03f-4db1-b733-121049dac2b6
Commits
-
Reviewing files that changed from the base of the PR and between a83c052 and dc6d32a.
Files selected for processing: 2
-
packages/adapter-libsql/src/errors.test.ts
packages/adapter-libsql/src/errors.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Remote libSQL errors come without a numeric rawCode, so they skipped the
275 case and fell back to P2039. Also recognise the CHECK constraint
message when the code is missing.
Signed-off-by: Raviteja Salva <[email protected]>
This branch has not been deployed
No deployments
Reviewers
At least 1 approving review is required to merge this pull request.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|