pub trait Type<DB>where
DB: Database,{
// Required method
fn type_info() -> <DB as Database>::TypeInfo;
// Provided method
fn compatible(ty: &<DB as Database>::TypeInfo) -> bool { ... }
}Expand description
Indicates that a SQL type is supported for a database.
ยงCompile-time verification
Type definitions are not verified against the database at compile-time.
The query!() macros have no implicit knowledge of user-defined types.
When using custom types in query parameters or output columns with query!(),
the use of type overrides is required.
struct MyUser { id: UserId, name: String }
// fetch all properties from user and override the type in Rust for `id`
let user = query_as!(MyUser, r#"SELECT users.*, id as "id: UserId" FROM users"#)
.fetch_one(&pool).await?;ยงDerivable
This trait can be derived by SQLx to support Rust-only wrapper types, enumerations, and (for
postgres) structured records. Additionally, an implementation of Encode and Decode is
generated.
ยงTransparent
Rust-only domain wrappers around SQL types. The generated implementations directly delegate to the implementation of the inner type.
#[derive(sqlx::Type)]
#[sqlx(transparent)]
struct UserId(i64);ยงNote: PgHasArrayType
If you have the postgres feature enabled, this derive also generates a PgHasArrayType impl
so that you may use it with Vec and other types that decode from an array in Postgres:
let user_ids: Vec<UserId> = sqlx::query_scalar("select '{ 123, 456 }'::int8[]")
.fetch(&mut pg_connection)
.await?;However, if you are wrapping a type that does not implement PgHasArrayType
(e.g. Vec itself, because we donโt currently support multidimensional arrays),
you may receive an error:
#[derive(sqlx::Type)] // ERROR: `Vec<i64>` does not implement `PgHasArrayType`
#[sqlx(transparent)]
struct UserIds(Vec<i64>);To remedy this, add #[sqlx(no_pg_array)], which disables the generation
of the PgHasArrayType impl:
#[derive(sqlx::Type)]
#[sqlx(transparent, no_pg_array)]
struct UserIds(Vec<i64>);ยงAttributes
#[sqlx(type_name = "<SQL type name>")]on struct definition: instead of inferring the SQL type name from the inner field (in the above case,BIGINT), explicitly set it to<SQL type name>instead. May trigger errors or unexpected behavior if the encoding of the given type is different than that of the inferred type (e.g. if you rename the above toVARCHAR). Affects Postgres only.#[sqlx(rename_all = "<strategy>")]on struct definition: Seederive docs in FromRow#[sqlx(no_pg_array)]: do not emit aPgHasArrayTypeimpl (see above).
ยงEnumeration
Enumerations may be defined in Rust and can match SQL by integer discriminant or variant name.
With #[repr(_)] the integer representation is used when converting from/to SQL and expects
that SQL type (e.g., INT). Without, the names of the variants are used instead and
expects a textual SQL type (e.g., VARCHAR, TEXT).
#[derive(sqlx::Type)]
#[repr(i32)]
enum Color { Red = 1, Green = 2, Blue = 3 }#[derive(sqlx::Type)]
#[sqlx(type_name = "color")] // only for PostgreSQL to match a type definition
#[sqlx(rename_all = "lowercase")]
enum Color { Red, Green, Blue }ยงRecords
User-defined composite types are supported through deriving a struct.
This is only supported for PostgreSQL.
#[derive(sqlx::Type)]
#[sqlx(type_name = "interface_type")]
struct InterfaceType {
name: String,
supplier_id: i32,
price: f64
}Required Methodsยง
Sourcefn type_info() -> <DB as Database>::TypeInfo
fn type_info() -> <DB as Database>::TypeInfo
Returns the canonical SQL type for this Rust type.
When binding arguments, this is used to tell the database what is about to be sent; which,
the database then uses to guide query plans. This can be overridden by Encode::produces.
A map of SQL types to Rust types is populated with this and used
to determine the type that is returned from the anonymous struct type from query!.
Provided Methodsยง
Sourcefn compatible(ty: &<DB as Database>::TypeInfo) -> bool
fn compatible(ty: &<DB as Database>::TypeInfo) -> bool
Determines if this Rust type is compatible with the given SQL type.
When decoding values from a row, this method is checked to determine if we should continue or raise a runtime type mismatch error.
When binding arguments with query! or query_as!, this method is consulted to determine
if the Rust type is acceptable.
Defaults to checking TypeInfo::type_compatible().
Dyn Compatibilityยง
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Typesยง
Sourceยงimpl Type<Any> for i16
impl Type<Any> for i16
fn type_info() -> AnyTypeInfo
fn compatible(ty: &AnyTypeInfo) -> bool
Sourceยงimpl Type<Any> for i32
impl Type<Any> for i32
fn type_info() -> AnyTypeInfo
fn compatible(ty: &AnyTypeInfo) -> bool
Sourceยงimpl Type<Any> for i64
impl Type<Any> for i64
fn type_info() -> AnyTypeInfo
fn compatible(ty: &AnyTypeInfo) -> bool
Sourceยงimpl Type<MySql> for Cow<'_, str>
impl Type<MySql> for Cow<'_, str>
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for IpAddr
impl Type<MySql> for IpAddr
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for bool
impl Type<MySql> for bool
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for f32
impl Type<MySql> for f32
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for f64
impl Type<MySql> for f64
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for i8
impl Type<MySql> for i8
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for i16
impl Type<MySql> for i16
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for i32
impl Type<MySql> for i32
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for i64
impl Type<MySql> for i64
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for str
impl Type<MySql> for str
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for u8
impl Type<MySql> for u8
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for u16
impl Type<MySql> for u16
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for u32
impl Type<MySql> for u32
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for u64
impl Type<MySql> for u64
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for Box<str>
impl Type<MySql> for Box<str>
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for Box<[u8]>
impl Type<MySql> for Box<[u8]>
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for String
impl Type<MySql> for String
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for Vec<u8>
impl Type<MySql> for Vec<u8>
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for Ipv4Addr
impl Type<MySql> for Ipv4Addr
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for Ipv6Addr
impl Type<MySql> for Ipv6Addr
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<MySql> for [u8]
impl Type<MySql> for [u8]
fn type_info() -> MySqlTypeInfo
fn compatible(ty: &MySqlTypeInfo) -> bool
Sourceยงimpl Type<Postgres> for Cow<'_, str>
impl Type<Postgres> for Cow<'_, str>
fn type_info() -> PgTypeInfo
fn compatible(ty: &PgTypeInfo) -> bool
Sourceยงimpl Type<Postgres> for IpAddr
impl Type<Postgres> for IpAddr
fn type_info() -> PgTypeInfo
fn compatible(ty: &PgTypeInfo) -> bool
Sourceยงimpl Type<Postgres> for str
impl Type<Postgres> for str
fn type_info() -> PgTypeInfo
fn compatible(ty: &PgTypeInfo) -> bool
Sourceยงimpl Type<Postgres> for ()
impl Type<Postgres> for ()
fn type_info() -> PgTypeInfo
fn compatible(ty: &PgTypeInfo) -> bool
Sourceยงimpl Type<Postgres> for Box<str>
impl Type<Postgres> for Box<str>
fn type_info() -> PgTypeInfo
fn compatible(ty: &PgTypeInfo) -> bool
Sourceยงimpl Type<Postgres> for String
impl Type<Postgres> for String
fn type_info() -> PgTypeInfo
fn compatible(ty: &PgTypeInfo) -> bool
Sourceยงimpl Type<Postgres> for TimeDelta
Available on crate feature chrono only.
impl Type<Postgres> for TimeDelta
chrono only.fn type_info() -> PgTypeInfo
Sourceยงimpl Type<Postgres> for Duration
Available on crate feature time only.
impl Type<Postgres> for Duration
time only.fn type_info() -> PgTypeInfo
Sourceยงimpl Type<Sqlite> for Cow<'_, str>
impl Type<Sqlite> for Cow<'_, str>
fn type_info() -> SqliteTypeInfo
fn compatible(ty: &SqliteTypeInfo) -> bool
Sourceยงimpl Type<Sqlite> for bool
impl Type<Sqlite> for bool
fn type_info() -> SqliteTypeInfo
fn compatible(ty: &SqliteTypeInfo) -> bool
Sourceยงimpl Type<Sqlite> for i8
impl Type<Sqlite> for i8
fn type_info() -> SqliteTypeInfo
fn compatible(ty: &SqliteTypeInfo) -> bool
Sourceยงimpl Type<Sqlite> for i16
impl Type<Sqlite> for i16
fn type_info() -> SqliteTypeInfo
fn compatible(ty: &SqliteTypeInfo) -> bool
Sourceยงimpl Type<Sqlite> for i32
impl Type<Sqlite> for i32
fn type_info() -> SqliteTypeInfo
fn compatible(ty: &SqliteTypeInfo) -> bool
Sourceยงimpl Type<Sqlite> for i64
impl Type<Sqlite> for i64
fn type_info() -> SqliteTypeInfo
fn compatible(ty: &SqliteTypeInfo) -> bool
Sourceยงimpl Type<Sqlite> for u8
impl Type<Sqlite> for u8
fn type_info() -> SqliteTypeInfo
fn compatible(ty: &SqliteTypeInfo) -> bool
Sourceยงimpl Type<Sqlite> for u16
impl Type<Sqlite> for u16
fn type_info() -> SqliteTypeInfo
fn compatible(ty: &SqliteTypeInfo) -> bool
Sourceยงimpl Type<Sqlite> for u32
impl Type<Sqlite> for u32
fn type_info() -> SqliteTypeInfo
fn compatible(ty: &SqliteTypeInfo) -> bool
Sourceยงimpl Type<Sqlite> for u64
impl Type<Sqlite> for u64
fn type_info() -> SqliteTypeInfo
fn compatible(ty: &SqliteTypeInfo) -> bool
Sourceยงimpl Type<Sqlite> for Box<[u8]>
impl Type<Sqlite> for Box<[u8]>
fn type_info() -> SqliteTypeInfo
fn compatible(ty: &SqliteTypeInfo) -> bool
Sourceยงimpl Type<Sqlite> for Vec<u8>
impl Type<Sqlite> for Vec<u8>
fn type_info() -> SqliteTypeInfo
fn compatible(ty: &SqliteTypeInfo) -> bool
Sourceยงimpl Type<Sqlite> for [u8]
impl Type<Sqlite> for [u8]
fn type_info() -> SqliteTypeInfo
fn compatible(ty: &SqliteTypeInfo) -> bool
Sourceยงimpl<T1, T2, T3> Type<Postgres> for (T1, T2, T3)
impl<T1, T2, T3> Type<Postgres> for (T1, T2, T3)
fn type_info() -> PgTypeInfo
Sourceยงimpl<T1, T2, T3, T4> Type<Postgres> for (T1, T2, T3, T4)
impl<T1, T2, T3, T4> Type<Postgres> for (T1, T2, T3, T4)
fn type_info() -> PgTypeInfo
Sourceยงimpl<T1, T2, T3, T4, T5> Type<Postgres> for (T1, T2, T3, T4, T5)
impl<T1, T2, T3, T4, T5> Type<Postgres> for (T1, T2, T3, T4, T5)
fn type_info() -> PgTypeInfo
Sourceยงimpl<T1, T2, T3, T4, T5, T6> Type<Postgres> for (T1, T2, T3, T4, T5, T6)
impl<T1, T2, T3, T4, T5, T6> Type<Postgres> for (T1, T2, T3, T4, T5, T6)
fn type_info() -> PgTypeInfo
Sourceยงimpl<T1, T2, T3, T4, T5, T6, T7> Type<Postgres> for (T1, T2, T3, T4, T5, T6, T7)
impl<T1, T2, T3, T4, T5, T6, T7> Type<Postgres> for (T1, T2, T3, T4, T5, T6, T7)
fn type_info() -> PgTypeInfo
Sourceยงimpl<T1, T2, T3, T4, T5, T6, T7, T8> Type<Postgres> for (T1, T2, T3, T4, T5, T6, T7, T8)
impl<T1, T2, T3, T4, T5, T6, T7, T8> Type<Postgres> for (T1, T2, T3, T4, T5, T6, T7, T8)
fn type_info() -> PgTypeInfo
Sourceยงimpl<T1, T2, T3, T4, T5, T6, T7, T8, T9> Type<Postgres> for (T1, T2, T3, T4, T5, T6, T7, T8, T9)
impl<T1, T2, T3, T4, T5, T6, T7, T8, T9> Type<Postgres> for (T1, T2, T3, T4, T5, T6, T7, T8, T9)
fn type_info() -> PgTypeInfo
Sourceยงimpl<T> Type<Postgres> for [T]where
T: PgHasArrayType,
impl<T> Type<Postgres> for [T]where
T: PgHasArrayType,
fn type_info() -> PgTypeInfo
fn compatible(ty: &PgTypeInfo) -> bool
Sourceยงimpl<T> Type<Postgres> for Vec<T>where
T: PgHasArrayType,
impl<T> Type<Postgres> for Vec<T>where
T: PgHasArrayType,
fn type_info() -> PgTypeInfo
fn compatible(ty: &PgTypeInfo) -> bool
Sourceยงimpl<T, const N: usize> Type<Postgres> for [T; N]where
T: PgHasArrayType,
impl<T, const N: usize> Type<Postgres> for [T; N]where
T: PgHasArrayType,
fn type_info() -> PgTypeInfo
fn compatible(ty: &PgTypeInfo) -> bool
Implementorsยง
impl Type<MySql> for MySqlTime
impl Type<MySql> for DateTime<Local>
impl Type<MySql> for DateTime<Utc>
impl Type<MySql> for NaiveDate
impl Type<MySql> for NaiveDateTime
impl Type<MySql> for NaiveTime
impl Type<MySql> for BigDecimal
impl Type<MySql> for Decimal
impl Type<MySql> for Uuid
impl Type<MySql> for Date
impl Type<MySql> for OffsetDateTime
impl Type<MySql> for PrimitiveDateTime
impl Type<MySql> for Time
impl Type<MySql> for Hyphenated
impl Type<MySql> for Simple
impl Type<Postgres> for PgCube
impl Type<Postgres> for IpNet
impl Type<Postgres> for IpNetwork
impl Type<Postgres> for Oid
impl Type<Postgres> for PgBox
impl Type<Postgres> for PgCiText
impl Type<Postgres> for PgCircle
impl Type<Postgres> for PgHstore
impl Type<Postgres> for PgInterval
impl Type<Postgres> for PgLQuery
impl Type<Postgres> for PgLSeg
impl Type<Postgres> for PgLTree
impl Type<Postgres> for PgLine
impl Type<Postgres> for PgMoney
impl Type<Postgres> for PgPath
impl Type<Postgres> for PgPoint
impl Type<Postgres> for PgPolygon
impl Type<Postgres> for PgRange<i32>
impl Type<Postgres> for PgRange<i64>
impl Type<Postgres> for PgRange<NaiveDate>
chrono only.impl Type<Postgres> for PgRange<NaiveDateTime>
chrono only.impl Type<Postgres> for PgRange<BigDecimal>
bigdecimal only.impl Type<Postgres> for PgRange<Decimal>
rust_decimal only.impl Type<Postgres> for PgRange<Date>
time only.impl Type<Postgres> for PgRange<OffsetDateTime>
time only.impl Type<Postgres> for PgRange<PrimitiveDateTime>
time only.impl Type<Postgres> for PgTimeTz
impl Type<Postgres> for PgTimeTz<NaiveTime, FixedOffset>
impl Type<Postgres> for NaiveDate
impl Type<Postgres> for NaiveDateTime
impl Type<Postgres> for NaiveTime
impl Type<Postgres> for MacAddress
impl Type<Postgres> for BigDecimal
impl Type<Postgres> for BitVec
impl Type<Postgres> for Decimal
impl Type<Postgres> for Uuid
impl Type<Postgres> for Date
impl Type<Postgres> for OffsetDateTime
impl Type<Postgres> for PrimitiveDateTime
impl Type<Postgres> for Time
impl Type<Sqlite> for NaiveDate
impl Type<Sqlite> for NaiveDateTime
impl Type<Sqlite> for NaiveTime
impl Type<Sqlite> for Uuid
impl Type<Sqlite> for Date
impl Type<Sqlite> for OffsetDateTime
impl Type<Sqlite> for PrimitiveDateTime
impl Type<Sqlite> for Time
impl Type<Sqlite> for Hyphenated
impl Type<Sqlite> for Simple
impl<DB> Type<DB> for Value
impl<DB> Type<DB> for BString
impl<DB> Type<DB> for RawValue
impl<T> Type<MySql> for Json<T>
impl<T> Type<MySql> for Text<T>
impl<T> Type<Postgres> for Json<T>
impl<T> Type<Postgres> for Text<T>
impl<T> Type<Sqlite> for Json<T>
impl<T> Type<Sqlite> for Text<T>
impl<Tz> Type<Postgres> for PgRange<DateTime<Tz>>where
Tz: TimeZone,
chrono only.