Working with Field Attributes
1. Using @id Attribute
| Aspect | Notes |
|---|---|
| Purpose | Marks field as primary key |
| Combine | Usually with @default(...) |
| Mongo | Must use @map("_id") |
2. Using @unique Attribute
| Option | Description |
|---|---|
map | Custom DB constraint name: @unique(map: "uq_users_email") |
sort | Asc | Desc (PG/MSSQL) |
length | Index prefix length (MySQL) |
3. Using @default Attribute
| Default Source | Example |
|---|---|
| Literal | @default(0), @default("draft") |
| Function | @default(now()) |
| Enum value | @default(ACTIVE) |
| Raw SQL | @default(dbgenerated("gen_random_uuid()")) |
4. Using @map Attribute
| Scope | Attribute | Effect |
|---|---|---|
| Field | @map("col_name") | Column name in DB |
| Model | @@map("table_name") | Table name in DB |
| Enum value | VALUE @map("db_value") | Stored representation |
5. Using @updatedAt Attribute
updatedAt DateTime @updatedAt
| Behavior | Detail |
|---|---|
| Auto-set | Prisma writes new Date() on every update |
| Initial | Set on create as well |
| Override | Pass explicit value to bypass |
6. Using @relation Attribute
| Option | Description |
|---|---|
fields | Local FK field(s) |
references | Referenced field(s) on related model |
name | Relation label for disambiguation |
onDelete | Cascade | Restrict | SetNull | SetDefault | NoAction |
onUpdate | Same options as onDelete |
map | FK constraint name |
7. Using @ignore Attribute
| Use | Effect |
|---|---|
| Field-level | Excluded from generated client |
Model-level @@ignore | Schema retained but unavailable to client |
| Common case | Legacy tables without PK after introspection |
8. Using @db Attribute
| Example | Native Type |
|---|---|
@db.VarChar(255) | varchar(255) |
@db.Text | text |
@db.Uuid | uuid |
@db.Timestamptz(6) | timestamp with tz |
@db.Money | money |
9. Combining Multiple Attributes
id String @id @default(uuid(7)) @db.Uuid @map("user_id")
email String @unique(map: "uq_users_email") @db.Citext
| Rule | Detail |
|---|---|
| Order | Free; reads left-to-right |
| Conflicting defaults | Last wins (validator warns) |
10. Understanding Attribute Precedence
| Precedence | Detail |
|---|---|
| @id vs @unique | @id implies unique; cannot combine on same field |
| @default vs explicit value | Explicit value in create wins over default |
| @@id vs @id | Mutually exclusive per model |
Field @map vs DB introspection | Schema mapping wins |