Model compound identity
A record is uniquely determined by two or more values, such as a user and a post in a join model.
Prisma · Const
@@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.
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.
@@id(_ fields: FieldReference[], name: String?, map: String?)| Name | Type | Description |
|---|---|---|
fields | FieldReference[] | The scalar fields that together form the composite primary key. |
name | String? | Optional custom name for the composite ID in Prisma Client. |
map | String? | Optional database constraint name mapped by Prisma. |
Defines a composite primary key for the model; Prisma Client exposes the composite identifier for query operations.
model Like {
postId Int
userId Int
@@id([postId, userId])
}This model uses the pair of fields as one composite primary key.
A record is uniquely determined by two or more values, such as a user and a post in a join model.
A database table already uses a multi-column primary key that the Prisma schema must represent.
Application code needs a readable composite-key selector instead of Prisma's generated field-name combination.
@@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.
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]).
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@mapPRIMARY KEY