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

Prisma · Const

@@index

@@index declares a database index on one or more fields of a Prisma model. Its options and supported behaviors vary by database provider.

How it works

Place @@index on a model to declare a database index over one or more fields. The field list can be written positionally or with the fields: name, and optional arguments such as map, where, sort, length, or type depend on the supported database and index configuration.

Syntax

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

Parameters

NameTypeDescription
fieldsFieldReference[]One or more model field references included in the index; the fields argument name may be omitted.
mapString?Optional database index name mapping.
whereraw(String) | { field: value }?Optional partial-index predicate, expressed as raw SQL or a type-safe object.

Returns

Declares an index in the database.

Key points

  • @@index adds a database index to the model.
  • You may write the fields array directly without the fields: label.
  • A single declaration can cover multiple fields for relational database models.
  • Per-field length and sort settings are placed inside the indexed field list.
  • The type argument configures the index access method where the provider supports it.

Common mistakes

  • Partial-index predicates use either a raw SQL expression or a type-safe object literal; choose the form supported by the documentation and database.
  • Composite indexes are documented for relational databases; do not assume the same multi-field behavior applies to every provider.

Example: Composite model index

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

  @@index([title, content])
}

This model declares a database index spanning the title and content fields.

When to use it

Speed up common lookups

Queries frequently filter or sort by a model field.

Create a multi-column index

A relational query benefits from a combined lookup across fields.

Configure an advanced index

The database requires a provider-specific access method or predicate.

Interview questions

QWhat does @@index do?

@@index declares an index in the database at the model level. Its field list can contain one field or multiple fields, such as @@index([title, content]).

Follow-up: How would you name the underlying database index? Use the optional map argument to control the database constraint or index name.

QHow is the field list specified?

The positional form @@index([title, author]) is equivalent to @@index(fields: [title, author]); the fields argument name is optional. Field-level length and sort options are written alongside the relevant field.

Follow-up: How can a MongoDB composite-type field be indexed? Use dot notation such as @@index([address.number]) where supported.

QAre all @@index options portable across databases?

Index configuration varies by database. For example, PostgreSQL supports access methods such as Hash, Gist, Gin, SpGist, and Brin through the type argument, while MySQL supports length configuration for indexed String and Bytes subparts.

Follow-up: What should be checked before using an advanced argument? Verify that the target database provider supports that argument on @@index.

  • @id
  • @@id
  • @unique
  • @@unique
  • @@fulltext
  • map
  • where
  • length

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