Defining Schema Models
1. Creating Model Definitions
| Element | Required | Notes |
|---|---|---|
| Identifier field | Yes | @id or @@id |
| Unique field | No | Recommended for lookups |
| Timestamps | No | createdAt / updatedAt convention |
2. Defining Field Names
| Rule | Detail |
|---|---|
| Convention | camelCase |
| Allowed chars | Letters, digits, underscore; must start with letter |
| Column mapping | @map("snake_case") |
3. Setting Field Types
| Scalar Type | JS/TS Type | Default DB Type (PG) |
|---|---|---|
String | string | text |
Boolean | boolean | boolean |
Int | number | integer |
BigInt | bigint | bigint |
Float | number | double precision |
Decimal | Decimal.js | decimal(65,30) |
DateTime | Date | timestamp(3) |
Json | JsonValue | jsonb |
Bytes | Uint8Array | bytea |
4. Using Type Modifiers
| Modifier | Syntax | Meaning |
|---|---|---|
| Optional | String? | Nullable column |
| List | String[] | Array (PG/Mongo only) |
| Required | String | NOT NULL (default) |
5. Adding Default Values
| Default | Example |
|---|---|
| Literal | @default("active") |
now() | DateTime current timestamp |
autoincrement() | Int sequence |
uuid() / uuid(7) | UUID v4 / v7 string |
cuid() / cuid(2) | CUID identifier |
nanoid(n) | NanoID string |
dbgenerated("expr") | Raw SQL default |
6. Setting Auto-Increment Fields
id Int @id @default(autoincrement())
| Note | Detail |
|---|---|
| PG | Generates a SERIAL/IDENTITY column |
| MongoDB | Not supported — use ObjectId |
| CockroachDB | Prefer sequence() |
7. Using UUID Generation
| Variant | Behavior |
|---|---|
uuid() | UUID v4 (random) |
uuid(7) NEW | UUID v7 (time-ordered, index-friendly) |
| Native type | @db.Uuid on PostgreSQL |
8. Using CUID Generation
| Variant | Description |
|---|---|
cuid() | CUID v1 (25 chars) |
cuid(2) | CUID2: shorter, secure, URL-safe |
| Use case | Distributed-safe collision-resistant IDs |
9. Creating Unique Fields
| Approach | Syntax |
|---|---|
| Single field | email String @unique |
| Composite | @@unique([firstName, lastName]) |
| Named | @@unique([a,b], name: "ab_uq") |
10. Adding Field Documentation
model User {
/// Primary email used for login
email String @unique
}
| Comment | Surfaces In |
|---|---|
/// | Generated client JSDoc, Prisma Studio tooltips |
// | Schema only |