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

Docs: update query!() documentation WRT casting parameters using _ #1133

Closed
AleVul opened this issue Mar 25, 2021 · 5 comments
Closed

Docs: update query!() documentation WRT casting parameters using _ #1133

AleVul opened this issue Mar 25, 2021 · 5 comments
Labels
documentation Improvements or additions to documentation E-easy good first issue Good for newcomers

Comments

@AleVul
Copy link

AleVul commented Mar 25, 2021

I have trouble using (or understanding how to use) sqlx::Type. I have derived it for a wrapper around a String.
As far as I understand, this should implement Encode and Decode traits for the wrapper type and as a consequence I should be able to pass an instance of the wrapper type to query! macro, but I get an error stating that the drivers expects an &str instead of the wrapper type.
The description on the Encode trait made me think that this is how it should work. If it is not please help me understand.

See this repo for a minimal example that leads to error.

@jplatte
Copy link
Contributor

jplatte commented Mar 25, 2021

I had a look at the linked repo. You have this:

sqlx::query!("INSERT INTO foo (email) VALUES ($1)", email)

since your email variable has a type of Email which is not a type SQLx knows about, it will complain that it's not the right type for the email column. You need to use a type override to disable this check:

sqlx::query!("INSERT INTO foo (email) VALUES ($1)", email as _)

@AleVul
Copy link
Author

AleVul commented Mar 26, 2021

@jplatte Thank you, that did the trick!
Though the description you linked to is somewhat misleading, I'm referring to this part

Casts to _ are forbidden for now as they produce rather nasty type errors.

Is it forbidden or not? Because I can definitely use it. Maybe an update to the docs is in order?

@jplatte
Copy link
Contributor

jplatte commented Mar 26, 2021

Yeah, seems like it. I think first sentence after the codeblock applies (i.e. as _ is available since Rust 1.45).

@abonander abonander changed the title Trouble using sqlx::Type Docs: update query!() documentation WRT casting parameters using _ Apr 28, 2021
@abonander
Copy link
Collaborator

Yeah, now we just treat as _ as a blanket override and remove it. That paragraph needs to be rewritten.

@abonander abonander added documentation Improvements or additions to documentation E-easy good first issue Good for newcomers labels Apr 28, 2021
@abonander
Copy link
Collaborator

closed by #1619

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation E-easy good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants