Solution skill
Each model must have at least one unique criteria that has only required fields.
Complete solution
Fixes the Prisma schema validation error 'Each model must have at least one unique criteria that has only required fields' (P1012) by giving the model a @id, @unique, or composite @@id/@@unique on required fields. Use when prisma validate or generate fails with this message. Not for database connection errors or migration failures.
When it applies
Not specified by the original author.
Instructions
TL;DR: Add @id, @unique, or a composite @@id/@@unique to your model, on fields that are required (not optional). Then npx prisma validate passes. Prisma needs at least one unique way to address a single record.
error: Each model must have at least one unique criteria that has only required fields.Steps
Find the model the error names and give it a unique criterion. Pick the first option that fits:
model User { id Int @id @default(autoincrement()) email String @unique }or for composite keys:
model Membership { userId String orgId String @@id([userId, orgId]) }Expected: the criterion covers only required fields (no
?on any of them).Run validation:
npx prisma validateExpected:
The schema at prisma/schema.prisma is valid.Regenerate the client and continue your migration:
npx prisma generateExpected:
Generated Prisma Clientwith no validation errors.
When to use
prisma validate,prisma generate, orprisma migrate devfails with the exact unique-criteria message- You are modeling a join table or a legacy table with no primary key
When not to use
- The error is about a datasource URL or protocol: that is a different P1012 message
- You actually want keyless models (MongoDB): use
@@ignoreor the MongoDB connector rules instead
Compatibility
Prisma schema validation, all versions 2.x through 7.x. PostgreSQL, MySQL, SQL Server, SQLite.
Variant phrasing: Each model must have at least one unique criteria that has only required fields on a model where every field is optional
Making the unique field required is the point: @unique on an optional field still fails validation. Change the field to required or pick a different field.
Why it happens
Prisma Client's findUnique, update, and delete need a guaranteed-unique lookup. A model with no unique criterion cannot support those operations, so the schema is rejected at validation time before any database is touched.
Edge cases
- Legacy tables truly without a unique column: add a surrogate auto-increment id in the migration.
- Composite
@@uniqueon two optional fields still fails; every field in the criterion must be required. - Views (not tables): map them with
@@ignoreor query via raw SQL instead of modeling them as writable models.
Evidence
A long-standing Prisma issue asked why models without any unique field are rejected at validation time. The confirmed resolution from the Prisma team: every model needs at least one unique criterion over required fields, so queries like findUnique have something to target. The fix is to add @id to a primary key field, @unique to a naturally unique field, or @@id([a, b]) / @@unique([a, b]) for composite keys, all on required (non-optional) fields. After adding the attribute, validate and generate pass. Source: https://github.com/prisma/prisma/issues/7944
By @issueatlas · Version 1 · Exact version
Maintainer review
Checking maintainer review for this exact version…
A maintainer review records the library version checked. It does not assert that it is the latest upstream release. Reported outcomes are separate evidence.
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_ofKQmSXU0Bj2Tdqhxd-1wg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Each model must have at least one unique criteria that has only required fields. <span>— Vectle</span></a>