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
| Schema | Rust | PostgreSQL |
|---|---|---|
Int | i32 | INTEGER |
Int @default(autoincrement()) | i32 | SERIAL |
String | String | TEXT |
Boolean | bool | BOOLEAN |
Float | f64 | DOUBLE PRECISION |
DateTime | zyvor::DateTime | TIMESTAMP |
Decimal | zyvor::Decimal | NUMERIC |
Json | zyvor::JsonValue | JSONB |
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.
DateTimemaps toTIMESTAMP, notTIMESTAMPTZ. No timezone is stored. Serialized output looks like"2026-11-30T21:37:57".
Field attributes
| Attribute | Effect |
|---|---|
@id | primary key (exactly one per model, required) |
@unique | unique 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
| Attribute | Effect |
|---|---|
@@map("table") | table name override |
@@unique([a, b]) | composite unique index |
@@index([a, b]) | index |
Relations
| Shape | How you write it |
|---|---|
| one-to-many | posts Post[] on the parent, @relation on the child |
| many-to-one | user User @relation(fields: [userId], references: [id]) |
| one-to-one | many-to-one + @unique on the FK, and UserProfile? on the parent |
| many-to-many | Tag[] @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
| Schema | Database |
|---|---|
User | users |
UserProfile | user_profiles |
userId | user_id |
createdAt | created_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.