Repository navigation
Description
Bug description
When a nested create operation causes a unique constraint violation (P2002), the modelName in error.meta incorrectly reports the top-level model (the one .create() / .update() was called on) instead of the model where the constraint violation actually occurred.
How to reproduce
Given the following schema:
model AppMajorVersion {
id String @id @default(cuid())
appId String
number Int
versions AppVersion[]
@@unique([appId, number])
}
model AppVersion {
id String @id @default(cuid())
majorVersionId String
number Int
majorVersion AppMajorVersion @relation(fields: [majorVersionId], references: [id])
@@unique([majorVersionId, number])
}When executing a nested create:
await prisma.appVersion.create({
data: {
majorVersion: {
create: {
appId: "some-app-id",
number: 1, // duplicate — violates AppMajorVersion@@unique([appId, number])
},
},
number: 1,
},
});If the nested create on AppMajorVersion violates @@unique([appId, number]), the resulting PrismaClientKnownRequestError has:
{
"code": "P2002",
"meta": {
"modelName": "AppVersion", // ❌ WRONG — should be "AppMajorVersion"
"target": ["appId", "number"]
}
}The target fields are correct (they reflect the actual database constraint), but modelName points to the top-level model (AppVersion) rather than the nested model (AppMajorVersion) where the violation occurred.
Expected behavior
error.meta.modelName should be "AppMajorVersion" — the model that owns the violated constraint.
Prisma information
Environment: Prisma 6.x with driver adapters (@prisma/adapter-pg)
Root cause analysis
The issue is in packages/client/src/runtime/RequestHandler.ts in handleRequestError():
const meta = modelName ? { modelName, ...error.meta } : error.metaThe modelName is always the top-level model from the client operation (passed via params.modelName from getPrismaClient.ts). It does not account for which model in a nested write actually triggered the constraint violation.
The engine-level error (UniqueKeyViolation in libs/user-facing-errors/src/query_engine/mod.rs) only carries a DatabaseConstraint (the fields), not a model name. The client injects modelName based solely on which model the operation was called on, regardless of nesting depth.
This was introduced in #20724 which added modelName to error meta but only handles top-level operations correctly.
Environment & setup
- OS: Linux
- Database: PostgreSQL (via driver adapter)
- Node.js version: 22.x
- Prisma version: 6.x
Activity
fix(p2002): correct modelName in nested create unique constraint errors
fix(p2002): correct modelName in nested create unique constraint errors
test(api-e2e): measure whether a P2002 identifies its constraint at all
test(api-e2e): pin the IP-tier throttle limit in the API child's env
feat(database): normalize a P2002 to its constraint identity (#292)
test(api-e2e): pin the P2002 payload shape against a real database (#298
fix(database): prefer the driver payload over meta.target, and guard …
refactor(database): split the utils barrel so each module can carry a…