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

Prisma · Const

@id

@id defines a model's single-field identifier. In relational databases it represents a single-field primary key; compound identifiers use @@id.

How it works

Use @id on one scalar field to make that field the model's single-field identifier and primary key. The field may also use a default generator such as autoincrement(); for MongoDB, the field must additionally map to the underlying _id field.

Syntax

fieldName Type @id

Parameters

NameTypeDescription
fieldscalar fieldOptional field-level arguments and modifiers can be added to the annotated field, such as a default value generator; the referenced page's @id section does not define a standalone parameter list.

Returns

Defines the model's single-field ID; it does not return a runtime value.

Key points

  • @id marks one field as the model's single-field identifier.
  • The ID distinguishes individual records within the model.
  • Relational databases use @id for a single-field primary key.
  • Use @@id instead when the identifier combines multiple fields.

Common mistakes

  • A model cannot define multiple separate @id fields; use @@id when the identifier is compound.
  • MongoDB requires the @id field to include @map("_id").

Example: Single-field model ID

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

This model gives each User one integer primary key generated by the database.

When to use it

Define a single-field primary key

A model needs one scalar primary key.

Combine with a default generator

Records need generated numeric identifiers.

Map MongoDB _id

A MongoDB model needs its required document identifier.

Interview questions

QWhat does @id do?

@id defines a single-field ID on a model. In relational databases, it represents the model's primary key, while a compound identifier uses @@id instead.

Follow-up: How would you model an identifier made from two fields? Use a model-level @@id([fieldA, fieldB]) declaration.

QHow many IDs can a Prisma model have?

A model may have only one ID. Put @id on the scalar field that uniquely identifies each record; use @unique for additional independently unique fields.

Follow-up: Can a model have both @id and @@id? No; the model has one ID, so choose either a single-field or compound definition.

QHow does @id differ across relational databases and MongoDB?

For MongoDB, the identifier must be a single field with @id and @map("_id"). Relational databases can use either a single-field @id or a multi-field @@id.

Follow-up: What is the relational alternative for a multi-field identifier? Declare @@id with the participating fields at model level.

  • @@id
  • @unique
  • @@unique
  • @default
  • @map

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