Trait sqlx_postgres::types::PgHasArrayType
source · pub trait PgHasArrayType {
// Required method
fn array_type_info() -> PgTypeInfo;
// Provided method
fn array_compatible(ty: &PgTypeInfo) -> bool { ... }
}
Expand description
Provides information necessary to encode and decode Postgres arrays as compatible Rust types.
Implementing this trait for some type T
enables relevant Type
,Encode
and Decode
impls
for Vec<T>
, &[T]
(slices), [T; N]
(arrays), etc.
§Note: #[derive(sqlx::Type)]
If you have the postgres
feature enabled, #[derive(sqlx::Type)]
will also generate
an impl of this trait for your type if your wrapper is marked #[sqlx(transparent)]
:
ⓘ
#[derive(sqlx::Type)]
#[sqlx(transparent)]
struct UserId(i64);
let user_ids: Vec<UserId> = sqlx::query_scalar("select '{ 123, 456 }'::int8[]")
.fetch(&mut pg_connection)
.await?;
However, this may cause an error if the type being wrapped does not implement PgHasArrayType
,
e.g. Vec
itself, because we don’t currently support multidimensional arrays:
ⓘ
#[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>);
See the documentation of Type
for more details.
Required Methods§
fn array_type_info() -> PgTypeInfo
Provided Methods§
fn array_compatible(ty: &PgTypeInfo) -> bool
Object Safety§
This trait is not object safe.