Define a single-field primary key
A model needs one scalar primary key.
Prisma · Const
@id defines a model's single-field identifier. In relational databases it represents a single-field primary key; compound identifiers use @@id.
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.
fieldName Type @id| Name | Type | Description |
|---|---|---|
field | scalar field | Optional 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. |
Defines the model's single-field ID; it does not return a runtime value.
model User {
id Int @id @default(autoincrement())
name String
}This model gives each User one integer primary key generated by the database.
A model needs one scalar primary key.
Records need generated numeric identifiers.
A MongoDB model needs its required document identifier.
@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.
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.
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