Skip to content

[Postgres] Unable to use #[derive]d custom types with Vec<T> #298

@jamwaffles

Description

@jamwaffles

Bit of a word soup of a title, sorry 😬

I'm trying to deserialize a row into a struct that holds a Vec<Item>, where Item is an enum that derives sqlx::Type. This fails to decode, stating some trait bounds aren't satisfied. Is this a not-yet-implement feature, or am I doing something wrong/missing something in my code?

Code:

use sqlx::postgres::PgQueryAs;

#[derive(
    serde_derive::Deserialize, serde_derive::Serialize, PartialEq, Debug, Eq, Hash, sqlx::Type, Copy, Clone
)]
#[sqlx(rename = "varchar")]
enum Item {
    Foo,
    Bar,
}

#[derive(
    serde_derive::Deserialize, serde_derive::Serialize, sqlx::FromRow, Hash, PartialEq, Eq, Debug, Clone
)]
struct Things {
    items: Vec<Item>,
}

async fn query_things(pool: &sqlx::PgPool) -> Result<Vec<Things>, sqlx::Error> {
    sqlx::query_as("select * from things")
        .fetch_all(&mut pool)
        .await
}

The pile of derives is from our real code. Could any of them be causing issues?

Error:

error[E0277]: the trait bound `std::vec::Vec<Item>: sqlx_core::types::Type<sqlx_core::postgres::database::Postgres>` is not satisfied
   --> src/lib.rs:250:10
    |
250 |         .fetch_all(&mut pool)
    |          ^^^^^^^^^ the trait `sqlx_core::types::Type<sqlx_core::postgres::database::Postgres>` is not implemented for `std::vec::Vec<Item>`
    |
    = help: the following implementations were found:
              <std::vec::Vec<&[u8]> as sqlx_core::types::Type<sqlx_core::postgres::database::Postgres>>
              <std::vec::Vec<&str> as sqlx_core::types::Type<sqlx_core::postgres::database::Postgres>>
              <std::vec::Vec<(T1, T2)> as sqlx_core::types::Type<sqlx_core::postgres::database::Postgres>>
              <std::vec::Vec<(T1, T2, T3)> as sqlx_core::types::Type<sqlx_core::postgres::database::Postgres>>
            and 23 others
    = note: required because of the requirements on the impl of `for<'c> sqlx_core::row::FromRow<'c, sqlx_core::postgres::row::PgRow<'c>>` for `Things`

error[E0277]: the trait bound `[Item]: sqlx_core::types::Type<sqlx_core::postgres::database::Postgres>` is not satisfied
   --> src/lib.rs:250:10
    |
250 |         .fetch_all(&mut pool)
    |          ^^^^^^^^^ the trait `sqlx_core::types::Type<sqlx_core::postgres::database::Postgres>` is not implemented for `[Item]`
    |
    = help: the following implementations were found:
              <[&[u8]] as sqlx_core::types::Type<sqlx_core::postgres::database::Postgres>>
              <[&str] as sqlx_core::types::Type<sqlx_core::postgres::database::Postgres>>
              <[(T1, T2)] as sqlx_core::types::Type<sqlx_core::postgres::database::Postgres>>
              <[(T1, T2, T3)] as sqlx_core::types::Type<sqlx_core::postgres::database::Postgres>>
            and 23 others
    = note: required because of the requirements on the impl of `sqlx_core::decode::Decode<'_, sqlx_core::postgres::database::Postgres>` for `std::vec::Vec<Item>`
    = note: required because of the requirements on the impl of `for<'c> sqlx_core::row::FromRow<'c, sqlx_core::postgres::row::PgRow<'c>>` for `Things`

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions