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

Scalar Types

The Eight Scalars

Zyvor supports eight scalar types:

SchemaRustPostgreSQL
Inti32INTEGER
StringStringTEXT
BooleanboolBOOLEAN
Floatf64DOUBLE PRECISION
DateTimezyvor::DateTimeTIMESTAMP
Decimalzyvor::DecimalNUMERIC
Jsonzyvor::JsonValueJSONB

Int with @default(autoincrement()) becomes SERIAL instead of INTEGER.

Important: Int maps to i32, not i64. 64-bit integer keys are not supported yet.

Nullable Fields

Append ? to a field’s type to make it nullable.

model User {
    bio String?  // Optional
    age Int      // Required
}
SchemaRustPostgreSQL
StringStringTEXT NOT NULL
String?Option<String>TEXT

Nullability in Three Places

Nullability affects reading, creating, and updating records.

1. Reading — Option<T>

Nullable fields become Option<T> in generated models:

#![allow(unused)]
fn main() {
pub bio: Option<String>
}

2. Creating — Some or None

When creating a record, provide either a value or None:

#![allow(unused)]
fn main() {
UserCreate {
    bio: Some("hello".into()),
    /* ... */
}
}

3. Updating — Two Layers of Option

Updates use two nested Option values to represent three possible states:

#![allow(unused)]
fn main() {
UserUpdate {
    bio: Some(Some("new".into())),
    /* ... */
}
}

The two layers have different meanings:

ValueMeaning
Some(Some(value))Set the column to this value
Some(None)Set the column to NULL
NoneLeave the column untouched

Three update states need two Option layers.

That’s the only genuinely new concept in Zyvor’s type system.

Nullable Fields Cannot Be Compared

Comparison operators such as eq, gt, and lt are currently available only for non-nullable String and Int fields.

A nullable field has a type such as Field<E, Option<String>>, which does not implement these comparison methods.

#![allow(unused)]
fn main() {
User::bio.eq("x")        // ✗ Does not compile
User::bio.is_null()      // ✓ Works
User::bio.is_not_null()  // ✓ Works
}

Null checks work on every field type.

See Query Builder for the complete operator table and workarounds.

Wrapper Types

DateTime, Decimal, and Json map to Zyvor’s own wrapper types rather than raw third-party types.

These wrappers implement:

  • Serialize
  • Deserialize
  • SQLx encode/decode traits

This means you don’t need to add chrono, rust_decimal, or serde_json to your Cargo.toml just to hold these values.

DateTime Is Copy

zyvor::DateTime implements Copy, so you can use it from a borrowed model without cloning:

#![allow(unused)]
fn main() {
created_at: user.created_at, // No .clone() needed
}

Check the derives on zyvor::Decimal in your generated output before assuming the same behaviour for it.

Accessing the Underlying Chrono Value

Zyvor does not re-export chrono.

If you need Chrono APIs for generating timestamps, formatting, or date arithmetic, you have two options.

Option 1: Add Chrono Directly

Add it to your Cargo.toml:

chrono = "0.4"

Option 2: Use SQLx’s Chrono Re-export

If your SQLx dependency has the chrono feature enabled, you can use its re-export:

#![allow(unused)]
fn main() {
use sqlx::types::chrono;

let email = format!(
    "ali_{}@test.com",
    chrono::Utc::now().timestamp()
);
}

This option adds no new dependency. That’s what the Axum example uses.

DateTime Has No Timezone

DateTime maps to PostgreSQL TIMESTAMP, not TIMESTAMPTZ.

Serialized output looks like this:

{
    "created_at": "2026-11-30T21:37:57"
}

There is:

  • No timezone offset.
  • No trailing Z.
  • No timezone information in the serialized timestamp.

If your API needs timezone-aware timestamps, store UTC by convention and document it. The database won’t enforce this convention for you.

Arrays: Parsed, Not Usable

Zyvor recognizes scalar arrays during parsing, but the CRUD generator currently rejects them.

model User {
    tags String[]  // Parses successfully, but CRUD generation rejects it
}

Scalar arrays are recognized by the parser but not supported by the CRUD generator.

Model arrays are different:

model User {
    posts Post[]  // Relation — supported
}

Model arrays represent relations and are fully supported.

If you need a list of strings today, use a child table or Json.


Next: Attributes