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

query_as! macro gives an error "trait From<i8> is not implemented for bool" #2790

Closed
JohnTheCoolingFan opened this issue Sep 29, 2023 · 2 comments
Labels

Comments

@JohnTheCoolingFan
Copy link

Bug Description

When using the query_as! macro with a struct that has a bool field, an error gets thrown saying that bool does not implement trait From<i8>.

Minimal Reproduction

sqlx::query!("CREATE TABLE test_table (test_bool_value BOOL NOT NULL)").execute(&pool).await.unwrap();

struct TestStruct {
    test_bool_value: bool,
}

let test_val = sqlx::query_as!(TestStruct, "SELECT test_bool_value FROM test_table").fetch_one(&pool).await.unwrap();

Info

  • SQLx version: 0.7.2
  • SQLx features enabled: runtime-tokio, tls-rustls, chrono, mysql
  • Database server and version: MariaDB 11.1.2
  • Operating system: Arch Linux x86_64
  • rustc --version: rustc 1.72.1 (d5c2e9c34 2023-09-13)
@JohnTheCoolingFan
Copy link
Author

Error was encountered in currently unpushed code when transitioning from methods to macros. Code I was trying to change is here: https://github.com/JohnTheCoolingFan/sayless/blob/118b1976d46eee71e625994839a11bb5cec42e6f/src/routes/create_link.rs#L23

@abonander
Copy link
Collaborator

This isn't really a bug, the behavior is intended just not well documented.

The problem is, all MySQL/MariaDB tells us is that the column is a TINYINT, as that's what it aliases BOOL/BOOLEAN to. We have no way to tell the difference from the wire protocol itself.

In this case, we could simply pull up the table definition, but that only works if the value actually originates from a table column and isn't generated by an expression.

The only real solution would be to put in the work to actually analyze (not just parse) the query itself, which would be quite the ordeal, and isn't even in the cards right now.

Not even #2648 would fix this as bool doesn't implement TryFrom<i8>.

I would recommend just using a type override in the query: https://docs.rs/sqlx/latest/sqlx/macro.query.html#force-a-differentcustom-type

For brevity, you could use a wildcard override: https://docs.rs/sqlx/latest/sqlx/macro.query_as.html#column-type-override-infer-from-struct-field

Alternatively, you could try query_as_unchecked!().

@abonander abonander closed this as not planned Won't fix, can't repro, duplicate, stale Oct 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants