Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Attributes

Attributes attach constraints and metadata. @ is per-field, @@ is per-model.

@id — primary key

id Int @id @default(autoincrement())

Exactly one per model, required. Effect: PRIMARY KEY in SQL, and the field is excluded from both Create and Update structs.

⚠️ Two hard constraints in v0.1

  1. The type must be Int. update and delete take id: i32 concretely.
  2. The column must be named id. Those methods emit WHERE id = $1 literally.
model Account {
    id Int @id @default(autoincrement())   // ✓ works
}

model Legacy {
    accountId Int @id @map("account_id")   // ✗ update/delete will fail
}

For a legacy table with a differently-named key, use update_many / delete_many (they take a condition) or raw SQL.

Composite @@id([a, b]) is not supported — use a surrogate id plus @@unique([...]).

@unique — unique constraint

email String @unique

Effect: UNIQUE in SQL. Violating it raises PostgreSQL 23505, which Zyvor classifies as Error::UniqueViolation — map that to HTTP 409. See Errors.

@unique on a foreign key is how you express one-to-one:

userId Int @unique    // one profile per user

@default(...) — three flavours

WrittenSQLIn Create struct?
@default(autoincrement())SERIAL❌ excluded
@default(now())DEFAULT NOW()❌ excluded
@default(0) / @default(false)DEFAULT 0 / DEFAULT false⚠️ still required

That third row surprises everyone. Given:

published Boolean @default(false)
viewCount Int     @default(0)

you still have to write:

#![allow(unused)]
fn main() {
PostCreate {
    published: true,   // required, despite the default
    view_count: 150,   // required, despite the default
    /* ... */
}
}

Why: Zyvor lists every non-excluded column in the INSERT, so the database default never gets a chance to apply. Only autoincrement() and now() are special-cased out. Making literal defaults optional is on the roadmap.

@map("...") — column rename

bio       String?  @map("user_bio")
createdAt DateTime @default(now()) @map("created_at")

Your Rust field keeps the schema name; the database column gets the mapped one.

LayerName
Schemabio
Rustpub bio: Option<String>
Generated attribute#[sqlx(rename = "user_bio")]
SQL column"user_bio"

Use it when your Rust naming and your database naming disagree.

⚠️ Keep mapped names lowercase snake_case. Migrations quote identifiers; the executor does not. @map("userName") creates a case-sensitive column that the executor then can’t find.

@@map("...") — table rename

model PostTag {
    // ...
    @@map("post_tags")
}

Overrides the automatic pluralized name. Essential for join tables and legacy schemas. Same lowercase rule applies.

@@unique([a, b]) — composite unique

model PostTag {
    id     Int @id @default(autoincrement())
    postId Int @map("post_id")
    tagId  Int @map("tag_id")

    @@unique([postId, tagId])
}

Effect: a unique index across both columns. This is what makes ON CONFLICT DO NOTHING work when inserting pivot rows — the same (post, tag) pair can’t be added twice.

@@index([a, b]) — index

@@index([userId, createdAt])

Effect: CREATE INDEX in the migration. No effect on generated Rust.

@relation(...)

Declares how models connect. Covered in its own page: Relations.

Parsed but inert

Recognised by the parser, not implemented downstream. Don’t rely on these:

AttributeStatus
@updatedAtparses; no automatic update behaviour generated
@default(cuid())parses; not translated to a SQL default

Quick reference

model Example {
    id        Int      @id @default(autoincrement())
    email     String   @unique
    status    Boolean  @default(false)
    bio       String?  @map("user_bio")
    createdAt DateTime @default(now()) @map("created_at")
    parentId  Int      @map("parent_id")

    parent Example @relation(fields: [parentId], references: [id])

    @@map("examples")
    @@unique([email, status])
    @@index([parentId, createdAt])
}

Next: Relations.