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

Prisma · Const

@unique

@unique defines a single-field unique constraint in a Prisma model. It can identify records through a scalar field and automatically corresponds to a unique database index.

How it works

Apply @unique to a scalar field to require distinct stored values and create a unique database constraint. It may be optional or required; however, when it is the sole unique identifier of a model without @id or @@id, the field must be required. Use @@unique for a constraint spanning multiple fields.

Syntax

@unique(map: String?, length: number?, sort: String?, clustered: Boolean?, where: raw(String) | { field: value }?)

Parameters

NameTypeDescription
mapString?Optional database constraint and index name override.
lengthnumber?Optional index length configuration.
sortString?Optional index sort direction.
clusteredBoolean?Optional clustered-index setting.
whereraw(String) | { field: value }?Optional filtered or partial-index predicate.

Key points

  • @unique makes one scalar field uniquely identify values within its model.
  • The attribute supports both nullable and non-nullable scalar fields.
  • You may mark multiple fields as unique in the same model.
  • Use @unique with scalar fields such as String, Int, or DateTime.
  • Schema application also creates the database's corresponding unique index.

Common mistakes

  • If a model has no @id or @@id, its sole @unique identifier cannot be optional.
  • An optional unique field can still have multiple null values.
  • Place @unique on scalar fields, not relation fields.

Example: Unique email field

model User {
  id    Int    @id @default(autoincrement())
  email String @unique
  name  String?
}

The required email field can uniquely identify each User record.

When to use it

Enforce unique email addresses

User accounts

Prevent duplicate provider IDs

External identifiers

Make a foreign key reference at most one related record

One-to-one relations

Interview questions

QWhat does @unique do, and when must the field be required?

@unique defines a unique constraint for one scalar field, so values in that column must be distinct. It may be optional or required, but it must be required when it is the only unique constraint on a model without an ID.

Follow-up: How would you enforce uniqueness across several fields? Use the model-level @@unique attribute with the participating scalar fields.

QCan a model contain multiple @unique fields?

A model can have any number of unique constraints, and @unique can be applied to any scalar field. Each application represents a single-field constraint rather than a compound constraint.

Follow-up: What is the corresponding syntax for a compound constraint? Use @@unique([fieldA, fieldB]) at model level.

QWhat database effect accompanies @unique?

Adding @unique automatically adds a corresponding unique index to the specified column. The database therefore receives both the uniqueness enforcement and its associated index through the schema definition.

Follow-up: How can the underlying constraint or index name be customized? Supply the optional map argument to @unique.

  • @@unique
  • @id
  • @@id
  • @map
  • @relation

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