Skip to content

#[derive(flatten)] doesn't work with query_as! macro #2188

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

Closed
miquels opened this issue Nov 4, 2022 · 5 comments
Closed

#[derive(flatten)] doesn't work with query_as! macro #2188

miquels opened this issue Nov 4, 2022 · 5 comments
Labels

Comments

@miquels
Copy link

miquels commented Nov 4, 2022

Bug Description

#[derive(flatten)] doesn't work with the query_as! macro. It fails to compile. The example code in the provided github repo does compile when the sqlx::query_as function is used.

$ cargo run
   Compiling sqlx-flatten-fails-with-query_as-macro v0.1.0 (/usr/home/miquels/rust/sqlx-flatten-fails-with-query_as-macro)
error[E0560]: struct `AccountKeyword` has no field named `name`
  --> src/main.rs:46:19
   |
46 |       let account = sqlx::query_as!(
   |  ___________________^
47 | |         AccountKeyword,
48 | |         "SELECT 1 as id, 'foo' as name, 'bar' as surname"
49 | |     )
   | |_____^ `AccountKeyword` does not have this field
   |
   = note: available fields are: `id`, `info`, `default`
   = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query_as` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0560]: struct `AccountKeyword` has no field named `surname`
  --> src/main.rs:46:19
   |
46 |       let account = sqlx::query_as!(
   |  ___________________^
47 | |         AccountKeyword,
48 | |         "SELECT 1 as id, 'foo' as name, 'bar' as surname"
49 | |     )
   | |_____^ `AccountKeyword` does not have this field
   |
   = note: available fields are: `id`, `info`, `default`
   = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query_as` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0560`.
error: could not compile `sqlx-flatten-fails-with-query_as-macro` due to 2 previous errors

Minimal Reproduction

repo: https://github.com/miquels/sqlx-flatten-fails-with-query_as-macro
code is in src/main.rs
reproduce: just do cargo run

Info

  • SQLx version: 0.6.2
  • SQLx features enabled: ["runtime-tokio-rustls", "sqlite" ] }
  • Database server and version: SQLite
  • Operating system: FreeBSD 13.1
  • rustc --version: rustc 1.64.0 (a55dd71d5 2022-09-19)
@miquels miquels added the bug label Nov 4, 2022
@CosmicHorrorDev
Copy link
Contributor

The query_as!() macro doesn't use FromRow. It just directly constructs the struct

I thought I remembered seeing that mentioned in the docs somewhere, but I can't find it now

@CosmicHorrorDev
Copy link
Contributor

Poked around some more and found that this is detailed in the docs for query_as!() (Don't know how I missed that before)

This macro does not use FromRow; in fact, no trait implementations are required at all, though this may change in future versions.

Along with providing a lot more details as to why (in short this allows for enforcing more checks at compile time)

@miquels
Copy link
Author

miquels commented Nov 5, 2022

I see, thanks for replying. I love the compile-time checks sqlx does, and I hoped it was just an oversight/bug that it could not be used with flatten. Feel free to close this issue, or maybe relabel it as 'feature request' or 'improvement'?

@abonander
Copy link
Collaborator

Long-standing feature request: #514

Closing as duplicate.

@caioluis
Copy link

How did you avoid this problem? I'm facing the same issue

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

4 participants