Overview
Attributes modify the behavior of fields and models. Field attributes start with@, while model (block) attributes start with @@.
Field Attributes
@id
Defines the primary key:@default
Sets default value for a field:autoincrement()- Auto-incrementing integernow()- Current timestampuuid()- Generate UUIDcuid()- Generate CUIDdbgenerated("SQL")- Database default expression
@unique
Ensures field values are unique:@relation
Defines relationships between models:Cascade- Delete/update related recordsNoAction- Prevent the operationSetNull- Set foreign key to nullSetDefault- Set to default valueRestrict- Prevent the operation (similar to NoAction)
@map
Maps field name to different column name:@updatedAt
Automatically updates timestamp on record modification:@db
Specifies native database type:@ignore
Excludes field from Prisma Client:Model Attributes
@@id
Defines composite primary key:@@unique
Defines unique constraint on multiple fields:@@index
Creates database index:@@map
Maps model name to different table name:@@ignore
Excludes model from Prisma Client:- Legacy tables not used in application
- Temporary migration tables
- Database views not managed by Prisma
Complete Examples
User Authentication
E-commerce Products
Social Media
Multi-tenant Application
Best Practices
- Always use
@idor@@idfor primary keys - Add
@uniqueconstraints for fields that should be unique - Use
@default(autoincrement())for auto-generated IDs - Include
@default(now())and@updatedAtfor timestamps - Use
@@mapto match existing database naming conventions - Add
@@indexon foreign keys and frequently queried fields - Use named constraints with
map:for better database management - Specify referential actions (
onDelete,onUpdate) explicitly - Use
@dbattributes for database-specific optimizations - Consider composite keys with
@@idfor join tables