Expand description
Mapping between Rust field types, SQL column types and Value.
DbType is implemented for every type a model field may have. The
Model derive uses DbType::SQL_TYPE and DbType::NULLABLE for
migration metadata and DbType::to_value / DbType::from_value for
writes and reads.
§Canonical storage forms
Backends without a native column type store a canonical form, and
from_value accepts both the native value and that form:
| Rust type | Native Value | Canonical fallback |
|---|---|---|
bool | Bool | Int 0/1 |
Decimal | Decimal | Text ("12.50"), Int, Float |
Uuid | Uuid | Text (hyphenated) or 16 Bytes |
NaiveDate | Date | Text DATE_FORMAT |
NaiveTime | Time | Text TIME_FORMAT |
DateTime<Utc> | Timestamp | Text TIMESTAMP_FORMAT (fixed width, so text order = time order) |
TimeDelta | Int (microseconds) | — |
IpAddr | Text | — |
serde_json::Value | Json | Text holding JSON |
Integer and float types also accept Decimal (integral, respectively any
value), because PostgreSQL returns numeric for SUM and AVG. On
SQLite, declare Decimal columns NUMERIC: the canonical text form would
otherwise sort and aggregate as text.
Enums§
- SqlType
- Portable column type family. Backends map it to concrete DDL.
Constants§
- DATE_
FORMAT - Text form of dates on backends without a native date type.
- TIMESTAMP_
FORMAT - Text form of UTC timestamps (always 6 fractional digits and
Z). - TIME_
FORMAT - Text form of times of day.
Traits§
- DbType
- A Rust type that can be stored in a model column.
Functions§
- canonical_
text - Canonical text for values a backend cannot store natively (see module docs).
- decode
- Decode column
columnofvalueasT, naming the column on failure.