Model Attributes
Model attributes are declared inside the model body and start with @@. They constrain the table as a whole rather than a single field.
Composite Unique Constraints and Indexes
model Membership {
id: int @id @auto
organization: string(64)
email: string
@@unique(organization, email)
@@index(email)
}2
3
4
5
6
7
8
@@id,@@unique, and@@indextake comma-separated bare field names;@@index([email])fails withField lists take bare names, not brackets.@viatakes exactly one owning relation name, while@@softDeletetakes at most one field name.stringwithout a length isstring(191), so string fields can be indexed, unique, or used as a string primary key without extra annotations, see WhystringDefaults to 191.textis unbounded and cannot be a key or indexed.
Composite Primary Keys
Primary key fields are immutable through the ORM, including composite key components and their bound relations. Use a surrogate primary key and a unique business key when identifiers must change.
model Membership {
account: Account
group: Group
role: "owner" | "member" = "member"
@@id(account, group)
}2
3
4
5
6
7
@@idcontains at least two non-nullable fields and may name relations, which stand for their foreign key fields; querying by primary key then requires all fields, for examplewhere: { accountId, groupId }.- A composite-key model may reference other models and accept
@viaback collections, but it cannot be the target of a foreign key or take part in a many-to-many relation.
Surrogate Keys Instead of Composite Keys
Foreign keys can only reference a single-field primary key, and @relation(field: ...) names exactly one column. If a model may ever be referenced by another model, give it a surrogate key instead of @@id:
- Use a primary key with no business meaning:
id: int @id @auto, orbigint, orstring(36) = uuid(). - Keep the former composite key as
@@unique(...)so the database still enforces it, and let other models reference the singleidcolumn.
Line Items
A line item is identified by its order and line number. Declare that pair as a unique constraint, and a refund needs only one foreign key column, lineId:
model Order {
id: int @id @auto
lines: OrderLine[] @via(order)
}
model OrderLine {
id: int @id @auto
order: Order @relation(onDelete: Cascade)
lineNo: int
quantity: int
refunds: Refund[] @via(line)
@@unique(order, lineNo)
}
model Refund {
id: int @id @auto
line: OrderLine
amount: decimal(10, 2)
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Associations with Extra Fields
An implicit many-to-many join table holds only the two foreign keys. When the association carries its own data, declare it as a regular model with a one-to-many relation from each side:
model User {
id: int @id @auto
memberships: Membership[] @via(user)
}
model Group {
id: int @id @auto
memberships: Membership[] @via(group)
}
model Membership {
id: int @id @auto
user: User
group: Group
role: "owner" | "member" = "member"
joinedAt: datetime = now()
@@unique(user, group)
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Multi-Tenancy
A child model stores both the tenant and its parent, so filtering by tenant needs no join:
model Tenant {
id: int @id @auto
slug: string(64) @unique
}
model Project {
id: int @id @auto
tenant: Tenant
key: string(16)
tasks: Task[] @via(project)
@@unique(tenant, key)
}
model Task {
id: int @id @auto
tenant: Tenant
project: Project
title: string(200)
@@index(tenant, project)
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Cross-table consistency is up to the application
With single-column foreign keys the database only checks that projectId exists, not that task.tenantId equals project.tenantId. When creating a task, read the tenant from the project instead of accepting both from the caller.
Name Mapping
@map and @@map set the column and table names in the database; client code still uses the names from the model:
model Account {
id: int @id @auto
email: string @unique @map("email_address")
createdAt: datetime = now() @map("created_at")
@@map("accounts")
}2
3
4
5
6
7
Migrations follow database names: renaming a field while keeping @map produces no migration, while changing the @map value drops the old column and adds a new one. Raw SQL must use the database names.
Comments
// comments are stored in the database: comment lines immediately before a model become the table comment, and a trailing comment on a field line becomes the column comment:
// Notes table
model Note {
id: int @id @auto // Primary key
title: string // Title
}2
3
4
5
Consecutive comment lines are joined with newlines; standalone lines between fields and /* */ blocks are not stored, and the limit is 1024 characters. Both MySQL and PostgreSQL persist comments, and migrations track them.
Soft Delete
@@softDelete(field) selects a nullable datetime or timestamp management field; @@softDelete() defaults to deletedAt and adds it when absent. See Soft Delete.
Type Declarations
Top-level type declarations describe json<T> values, not model attributes; they drive generated types and JSON validation, not physical columns. See Typed JSON.
Next Steps
- Models and Fields: field types, defaults, and field attributes
- Relations: one-to-many, one-to-one, many-to-many, and polymorphic relations