Skip to content
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

Can't use transparent type in a postgres ANY array #1744

Closed
carols10cents opened this issue Mar 10, 2022 · 1 comment · Fixed by #1748
Closed

Can't use transparent type in a postgres ANY array #1744

carols10cents opened this issue Mar 10, 2022 · 1 comment · Fixed by #1748

Comments

@carols10cents
Copy link
Contributor

What happened

I have a type that wraps a primitive type that is annotated with #[sqlx(transparent)], much like this one in the test suite:

#[sqlx(transparent)]
struct Transparent(i32);

I have a &[Transparent] that I want to be able to use in a query like SELECT ... WHERE something IN ($1) with $1 bound to my slice of Transparent values.

Per the relevant FAQ entry, I tried:

sqlx::query("SELECT ... WHERE something = ANY($1);")
    .bind(values) // values is &[Transparent]

but compiling this gives me the error the trait `PgHasArrayType` is not implemented for `Transparent`.

Interestingly, if I map out the inner primitive value by doing something like:

let primitive_values: Vec<_> = values.iter().map(|v| v.0).collect();
sqlx::query("SELECT ... WHERE something = ANY($1);")
    .bind(&primitive_values) // primitive_values is &[i32]

then I don't get the compiler error.

I expected both of these to compile and function in the same way.

I've tried to make a minimal test case in this PR I made to my own fork so that I could demonstrate the compiler error happens in CI.

I'm happy to submit that test case as a PR here if it's useful, but I wasn't sure enough that this use case is supported or that I'm doing what I'm supposed to be doing.

Thanks!

@jplatte
Copy link
Contributor

jplatte commented Mar 11, 2022

This should work if you manually implement PgHasArrayType for Transparent in terms of i32s PgHasArrayType implementation.

I think there's another issue somewhere about basically the same thing for custom enum types. This is just a matter of nobody having put in the work to derive that trait yet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants