Skip to main content
Version: 1.2.x 🚧

Column Types

Type mappings

The column type will be derived automatically with the following mapping.

SQL Server (MSSQL) backend

The type mappings of MSSQL can be found here.

For the mappings of Rust primitive data types:

Rust typeDatabase Type
(ColumnType)
SQLite
datatype
MySQL
datatype
PostgreSQL
datatype
StringCharcharcharchar
StringStringvarcharvarcharvarchar
i8TinyIntegertinyinttinyintchar
u8TinyUnsignedtinyinttinyint unsignedN/A
i16SmallIntegersmallintsmallintsmallint
u16SmallUnsignedsmallintsmallint unsignedN/A
i32Integerintegerintinteger
u32Unsignedintegerint unsignedN/A
i64BigIntegerbigintbigintbigint
u64BigUnsignedbigintbigint unsignedN/A
f32Floatfloatfloatreal
f64Doubledoubledoubledouble precision
boolBooleanbooleanboolbool
Vec<u8>Binaryblobblobbytea

For the mappings of Rust non-primitive data types. You can check entity/prelude.rs for all of the reexported types.

Rust typeDatabase Type
(ColumnType)
SQLite
datatype
MySQL
datatype
PostgreSQL
datatype
Date: chrono::NaiveDate
TimeDate: time::Date
Datedate_textdatedate
Time: chrono::NaiveTime
TimeTime: time::Time
Timetime_texttimetime
DateTime: chrono::NaiveDateTime
TimeDateTime: time::PrimitiveDateTime
DateTimedatetime_textdatetimetimestamp
DateTimeLocal: chrono::DateTime<Local>
DateTimeUtc: chrono::DateTime<Utc>
Timestamptimestamp_texttimestampN/A
DateTimeWithTimeZone: chrono::DateTime<FixedOffset>
TimeDateTimeWithTimeZone: time::OffsetDateTime
TimestampWithTimeZonetimestamp_with_timezone_texttimestamptimestamp with time zone
Uuid: uuid::Uuid, uuid::fmt::Braced, uuid::fmt::Hyphenated, uuid::fmt::Simple, uuid::fmt::UrnUuiduuid_textbinary(16)uuid
Json: serde_json::ValueJsonjson_textjsonjson
Decimal: rust_decimal::DecimalDecimalrealdecimaldecimal

You can override the default mappings between a Rust type and ColumnType with the column_type attribute.

#[sea_orm(column_type = "Text")]
pub name: String

JSON column

If you need your JSON field to be deserialized into a struct. You would need to derive FromJsonQueryResult for it.

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "json_struct")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
// JSON column as `serde_json::Value`
pub json: Json,
// JSON column as custom struct
pub json_value: KeyValue,
// nullable JSON column as custom struct, backed by jsonb (Postgres only)
#[sea_orm(column_type = "JsonBinary")]
pub json_value_opt: Option<KeyValue>,
// JSON column storing a vector of objects
pub json_value_vec: Vec<KeyValue>,
}

// The custom struct must derive `FromJsonQueryResult`, `Serialize` and `Deserialize`
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromJsonQueryResult)]
pub struct KeyValue {
pub id: i32,
pub name: String,
pub price: f32,
pub notes: Option<String>,
}

If you want a cross-database way of implementing array column, you can wrap it with a wrapper type.

#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "json_string_vec")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
// nullable JSON column storing a vector of string
pub str_vec: Option<StringVec>,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromJsonQueryResult)]
pub struct StringVec(pub Vec<String>);

More details and examples in the next chapter.

Postgres Array

Array datatype is a Postgres-only feature. You can define a vector of primitive types that is already supported by SeaORM.

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "collection")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub integers: Vec<i32>,
pub integers_opt: Option<Vec<i32>>,
pub floats: Vec<f32>,
pub doubles: Vec<f64>,
pub strings: Vec<String>,
}

Postgres Vector

Since 1.1.6

PgVector support is added. Requires postgres-vector feature flag.

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "image_model")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: i32,
pub embedding: PgVector,
}

For a complete example, see embedding_tests.