-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Postgres] Unable to use #[derive]d custom types with Vec<T> #298
Comments
I'm having the same issue. |
@jamwaffles I might be misunderstanding, but is your table |
Sorry for not being clearer in the OP. My table looks like this: create table my_table (
id uuid primary_key,
roles varchar array,
...
); With the matching Rust: #[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 MyThing {
id: Uuid,
roles: Vec<Item>,
// ...
} Hope that makes things clearer! |
Hi! I looked into this as it turns out it would useful for something I need. Unfortunately, it's blocked right now. impl<T> Type<Postgres> for Vec<T>
where
T: Type<Postgres>,
{
#[inline]
fn type_info() -> PgTypeInfo {
<T as Type<Postgres>>::type_info()
}
} But this dies because lazy normalization doesn't exist yet, (it tries to recursively resolve the trait forever) |
@izik1 wouldn't your proposed implementation return type info for Using just T, for a custom type called
In 0.3.5 stable. |
I see the workaround (that will hopefully soon not be needed anymore) isn't actually mentioned here (from #1170 (comment)): #[derive(sqlx::Encode)]
struct Foos<'a>(&'a [Foo]);
impl sqlx::Type<sqlx::Postgres> for Foos<'_> {
fn type_info() -> PgTypeInfo {
PgTypeInfo::with_name("_foo")
}
}
query_as!(
Whatever,
"<QUERY with $1 of type foo[]>",
Foos(&foo_vec) as _,
) |
@jplatte thanks for reporting here that comment. Would it also work when trying to implement
|
I would assume that also works if you change the type of the |
This uses `sqlx` to read/write humans from/to the sample-db. The schema consists of a custom enum type for episodes, and a table for humans. For now, the same structs as were used for the GraphQL schema are being used to (de)serialize database values. This would have been quite smooth, but sadly, `sqlx`'s derivation can't handle `Vec`s of custom enums (launchbadge/sqlx#298), so we have to jump through some hoops by introducing `EpisodeSlice` and `EpisodeVec` newtypes, which can then implement the required traits (plus all the required juniper traits, which is the bigger pain). Since we're using `sqlx`'s macros to check queries at compile time, we need to connect to the database during compilation. The macro will use the `DATABASE_URL` environment variable, which it can read from a `.env` file, so we now write one of these files as part of `make prepare-db` (note that this is for the benefit of editors, language servers, etc., `make run` would already inherit the `DATABASE_URL` when compiling).
That trick doesn't seem to work for Decode for me though. Any workaround there? |
That trick is no longer needed, #1385 fixed it. And |
#[derive(Serialize, Deserialize)]
pub struct CartItem {
pub id: i32,
pub quantity: i32,
pub price: i32
}
#[derive(sqlx::Type, Serialize, Deserialize)]
pub struct CartItems {
pub items: Vec<CartItem>,
}
#[derive(sqlx::Type, Serialize, Deserialize)]
pub struct Voucher {
pub id: i32,
pub voucher_id: String,
pub customer_name: Option<String>,
pub customer_contact: Option<String>,
pub cart_items: CartItems,
pub time: chrono::NaiveDateTime,
pub status: bool
} I can't get it to working. |
Still not working today although it is stated in the design doc that multi dimensional data is not supported atm. I ran into trouble with Vec<(String, String)> type and other similar but more complex types, had to resort to serialising to string trick to satisfy the Encode trait |
Bit of a word soup of a title, sorry 😬
I'm trying to deserialize a row into a struct that holds a
Vec<Item>
, whereItem
is an enum that derivessqlx::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:
The pile of derives is from our real code. Could any of them be causing issues?
Error:
The text was updated successfully, but these errors were encountered: