Skip to content

P2002 error reports incorrect modelName for nested create operations #29595

Closed

Description

@Stadly

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.meta

The 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions