0

Can I tell sqlx to ignore a field in a query I pass to query_as!()?

struct User {
    a:String
}
...
let users = sqlx::query_as(User, 
    r#"
        select a, b from my_table
    "#
).fetch_all(pool).await;

(obviously) results in:

error[E0560]: struct `User` has no field named `b`

Without resorting to a custom from_row(), is there a cleaner tag perhaps to "skip" like there is on the receiving struct side? (per https://docs.rs/sqlx/latest/sqlx/trait.FromRow.html).

[edit: the original sql was easily edited to avoid the question altogether; I'll leave the question since I'm still curious if there's an sqlx secret I can't find]

Chayim Friedman
  • 47,971
  • 5
  • 48
  • 77
cloudsurfin
  • 2,467
  • 2
  • 25
  • 29
  • 1
    Why can't you simply `order by case when [something]...end`? – cafce25 Aug 29 '23 at 18:35
  • I didn't know you can do that. Thanks. Is there basically never a reason sqlx would need this functionality? Therefore dumb question? – cloudsurfin Aug 29 '23 at 18:40
  • 1
    Not sure maybe if you're generating the SQL by some template or so. But it's definitely not really a feature I'd expect much demand for. And even then the solution would probably be have a better sql generator. – cafce25 Aug 29 '23 at 18:42
  • To simplify the question and keep it to sqlx I'll edit the original now that I've been schooled in sql. – cloudsurfin Aug 29 '23 at 18:56

0 Answers0