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

Prisma · Const

@@id

@@id defines a composite primary key using multiple scalar fields on a Prisma model. The combined field values identify each record and can be referenced through Prisma Client.

How it works

The @@id block defines a model's primary key from multiple scalar fields. Prisma treats the combined values as one identifier and exposes the composite key in Client queries using a generated name based on the participating fields, unless you provide a custom name.

Syntax

@@id(_ fields: FieldReference[], name: String?, map: String?)

Parameters

NameTypeDescription
fieldsFieldReference[]The scalar fields that together form the composite primary key.
nameString?Optional custom name for the composite ID in Prisma Client.
mapString?Optional database constraint name mapped by Prisma.

Returns

Defines a composite primary key for the model; Prisma Client exposes the composite identifier for query operations.

Key points

  • Use @@id to make several model fields jointly serve as the primary key.
  • The database identifies a record from the combined values of all fields listed in the block.
  • For relational connectors, @@id maps to the database's PRIMARY KEY concept.
  • You can write the compact form @@id([fieldA, fieldB]) instead of naming the fields argument.
  • Without a custom name, Prisma Client derives the composite-key selector name by joining field names with underscores.

Common mistakes

  • MongoDB cannot use @@id; its IDs must be represented by a single field with @id and @map("_id").
  • Fields participating in a composite ID cannot be optional.
  • Only scalar fields such as String, Int, and enum can participate directly; relation fields cannot.

Example: Define a composite ID

model Like {
  postId Int
  userId Int

  @@id([postId, userId])
}

This model uses the pair of fields as one composite primary key.

When to use it

Model compound identity

A record is uniquely determined by two or more values, such as a user and a post in a join model.

Reflect an existing relational key

A database table already uses a multi-column primary key that the Prisma schema must represent.

Name the Client selector

Application code needs a readable composite-key selector instead of Prisma's generated field-name combination.

Interview questions

QWhat does @@id do?

@@id defines a multi-field primary key on a model. The combined values identify a record, and the corresponding database type is a PRIMARY KEY.

Follow-up: How does Prisma Client name an unnamed composite key? By default, it combines the field names with underscores, such as postId_userId.

QHow do you declare a composite ID?

Use the fields argument with an array of scalar field names, for example @@id([postId, userId]). The fields argument itself may be omitted in this shorthand form.

Follow-up: How can the generated Prisma Client key name be changed? Add a name argument, such as @@id(name: "likeId", fields: [postId, userId]).

QWhat database limitation applies to @@id?

A composite ID is supported for relational databases but not by MongoDB. In MongoDB, a model must use one field marked with @id and mapped to _id.

Follow-up: Can a composite ID be placed on a relation field? No; the participating fields must be scalar fields.

  • @id
  • @@unique
  • @default
  • @map
  • PRIMARY KEY

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