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

Schema Syntax

Your schema lives at .zyvor/schema.zyvor.

A complete example

model User {
    id        Int      @id @default(autoincrement())
    name      String
    email     String   @unique
    age       Int
    bio       String?  @map("user_bio")
    createdAt DateTime @default(now()) @map("created_at")

    profile UserProfile?
    posts   Post[]
}

model UserProfile {
    id      Int     @id @default(autoincrement())
    avatar  String?
    website String?
    userId  Int     @unique
    user    User    @relation(fields: [userId], references: [id], onDelete: Cascade)
}

model Post {
    id        Int      @id @default(autoincrement())
    title     String
    content   String?
    published Boolean  @default(false)
    viewCount Int      @default(0) @map("view_count")
    userId    Int      @map("user_id")
    createdAt DateTime @default(now()) @map("created_at")

    user     User      @relation(fields: [userId], references: [id], onDelete: Cascade)
    comments Comment[]
    tags     Tag[]     @relation(through: "post_tags", pivotFrom: "post_id", pivotTo: "tag_id")
}

model Tag {
    id    Int    @id @default(autoincrement())
    name  String @unique
    posts Post[] @relation(through: "post_tags", pivotFrom: "tag_id", pivotTo: "post_id")
}

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

    post Post @relation(fields: [postId], references: [id], onDelete: Cascade)
    tag  Tag  @relation(fields: [tagId],  references: [id], onDelete: Cascade)

    @@unique([postId, tagId])
    @@map("post_tags")
}

Types

SchemaRustPostgreSQL
Inti32INTEGER
Int @default(autoincrement())i32SERIAL
StringStringTEXT
BooleanboolBOOLEAN
Floatf64DOUBLE PRECISION
DateTimezyvor::DateTimeTIMESTAMP
Decimalzyvor::DecimalNUMERIC
Jsonzyvor::JsonValueJSONB

T? makes it Option<T> / nullable.

The wrapper types re-export their inner crate, so you don’t need chrono, rust_decimal, or serde_json as direct dependencies just to hold a value. zyvor::DateTime and zyvor::Decimal are Copy — you can move them out of a &Model without cloning.

DateTime maps to TIMESTAMP, not TIMESTAMPTZ. No timezone is stored. Serialized output looks like "2026-11-30T21:37:57".

Field attributes

AttributeEffect
@idprimary key (exactly one per model, required)
@uniqueunique constraint
@default(autoincrement())SERIAL; excluded from Create
@default(now())DEFAULT NOW(); excluded from Create
@default(<literal>)SQL default — still required in Create
@map("col")column name override → #[sqlx(rename)]
@relation(...)relation metadata

Model attributes

AttributeEffect
@@map("table")table name override
@@unique([a, b])composite unique index
@@index([a, b])index

Relations

ShapeHow you write it
one-to-manyposts Post[] on the parent, @relation on the child
many-to-oneuser User @relation(fields: [userId], references: [id])
one-to-onemany-to-one + @unique on the FK, and UserProfile? on the parent
many-to-manyTag[] @relation(through: "post_tags", pivotFrom: ..., pivotTo: ...) on both sides

onDelete accepts Cascade, Restrict, SetNull, NoAction.

Note the camelCase: pivotFrom / pivotTo, not pivot_from.

Pivot tables must be explicit

Zyvor does not invent a join table from through:. You declare it as a model (PostTag above) so it gets a real CREATE TABLE, foreign keys, and a composite unique constraint. @@id([a, b]) is not supported — use a surrogate id plus @@unique([...]).

Naming conventions

SchemaDatabase
Userusers
UserProfileuser_profiles
userIduser_id
createdAtcreated_at

Table names are pluralized snake_case, columns are snake_case. @map / @@map always win. The same naming module feeds both the entity generator and the migration generator, so they can’t drift apart.

Parsed but not fully supported

Being explicit so you don’t lose an afternoon:

  • Scalar arrays (tags String[]) — parsed, but CRUD generation rejects them.
  • Composite foreign keys — metadata parses; codegen handles single-column only.
  • @updatedAt — parses; no automatic update behaviour is generated.
  • @@id([...]) — not supported.
  • cuid() defaults — parsed; not translated to a SQL default.