Define your model fields with various scalar types, modifiers, and attributes for complete control over your database schema.
Prax provides scalar types that map to native database types and Rust types:
| Prax Type | Rust Type | PostgreSQL | MySQL | SQLite |
|---|---|---|---|---|
Int |
i32 | INTEGER | INT | INTEGER |
BigInt |
i64 | BIGINT | BIGINT | INTEGER |
Float |
f64 | DOUBLE PRECISION | DOUBLE | REAL |
Decimal |
rust_decimal::Decimal | DECIMAL | DECIMAL | REAL |
String |
String | TEXT | VARCHAR(191) | TEXT |
Boolean |
bool | BOOLEAN | TINYINT(1) | INTEGER |
DateTime |
chrono::DateTime<Utc> | TIMESTAMP(3) | DATETIME(3) | TEXT |
Json |
serde_json::Value | JSONB | JSON | TEXT |
Bytes |
Vec<u8> | BYTEA | LONGBLOB | BLOB |
Modifiers change how field values are handled:
| Modifier | Syntax | Rust Type | Description |
|---|---|---|---|
| Required | String |
String | Must have a value (NOT NULL) |
| Optional | String? |
Option<String> | Can be NULL |
| Array | String[] |
Vec<String> | List of values |
| Optional Array | String[]? |
Option<Vec<String>> | Array that can be NULL |
Attributes modify field behavior and add constraints:
Every model needs a unique identifier. Choose the right primary key strategy for your use case:
Best for simple applications with a single database. Compact and fast.
Best for distributed systems, APIs, or when you need to generate IDs client-side.
Set default values to simplify record creation and ensure data consistency:
Track when records are created, updated, and deleted:
Store flexible, schema-less data in JSON fields:
Note: JSON fields are flexible but lack schema enforcement. Document the expected structure in comments and consider validation in your application layer.
Store multiple values in a single field (PostgreSQL native support, emulated on other databases):
Use Decimal for exact precision, especially for financial data:
Store binary data like files, images, or hashes:
Use @db.TypeName to specify
native database types for advanced use cases:
| Attribute | Description |
|---|---|
@id |
Marks field as primary key |
@auto |
Auto-increment (for Int primary keys) |
@unique |
Unique constraint on field |
@default(value) |
Default value (literal, function, or dbgenerated) |
@updatedAt |
Auto-update timestamp on record changes |
@map("name") |
Custom column name in database |
@db.Type |
Specific database column type |
@ignore |
Exclude from generated client |
@relation(...) |
Define relationship to another model |