Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Prisma · Const

@@unique

@@unique defines a compound unique constraint across specified model fields. Every participating field must be mandatory, and Prisma adds a corresponding unique index.

How it works

Place @@unique on a model to require that the combination of listed fields is unique. Prisma creates the corresponding unique index, and the fields must be mandatory; optional fields cannot participate in the constraint.

Syntax

@@unique(_ fields: FieldReference[], name: String?, map: String?, where: raw(String) | { field: value }?)

Parameters

NameTypeDescription
fieldsFieldReference[]Fields making up the compound constraint; every field must be mandatory.
nameString?Optional Prisma-level name for the constraint.
mapString?Optional database-level mapped name.
whereraw(String) | { field: value }?Optional raw filter expression or field-value object.

Returns

A model-level compound unique constraint and corresponding unique index.

Key points

  • It enforces uniqueness on a combination of fields at the model level.
  • Several different field combinations can each have their own compound constraint.
  • The declaration also results in a unique index for the selected database columns.
  • The fields list can include a length modifier for supported fields.
  • Field entries can specify descending or ascending sort order.

Common mistakes

  • Do not include optional fields in the compound constraint.
  • A model without an ID needs this block when it is the model's sole unique identifier.

Example: Compound uniqueness for a model

model Post {
  id       Int    @id @default(autoincrement())
  authorId Int
  title    String

  @@unique([authorId, title])
}

Ensures an author cannot have two posts with the same title.

When to use it

Scoped uniqueness

Prevent duplicate titles within the same author or owner.

Compound business identifier

Represent a natural key made from multiple required columns.

Database naming

Control generated constraint and index names.

Interview questions

QWhat does @@unique do?

@@unique defines a compound unique constraint across multiple model fields. Prisma also adds a corresponding unique index for those columns.

Follow-up: What restriction applies to the participating fields? They must all be mandatory, not optional.

QHow do you declare a compound unique constraint?

Use the fields argument with a list such as @@unique([authorId, title]). The combination, rather than either field independently, must be unique.

Follow-up: How would you make the database object name explicit? Supply the name or map argument in the attribute.

QHow many @@unique blocks can a model have, and when is one required?

A model can contain multiple @@unique blocks. If it has no @id or @@id, a @@unique block is required when it is the model's only unique constraint.

Follow-up: Can @@unique replace every identifier requirement? No; the model still needs at least one unique identifier, and the documented rule applies when it is the sole one.

  • @unique
  • @@id
  • @id
  • unique index
  • compound unique constraint

Source: Prisma Schema API (Prisma ORM v7). Checked 2026-10-16.