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! and query_file_as! macros ignore #[sqlx(default)] #1140

Closed
OriolMunoz opened this issue Mar 27, 2021 · 1 comment
Closed

query_as! and query_file_as! macros ignore #[sqlx(default)] #1140

OriolMunoz opened this issue Mar 27, 2021 · 1 comment
Labels
duplicate This issue or pull request already exists

Comments

@OriolMunoz
Copy link
Contributor

OriolMunoz commented Mar 27, 2021

Support for #[sqlx(default)] was implemented in #495, but it only affects usages of the non-macro sqlx::query_as(...).
However, this doesn't work for the macros query_as! and query_file_as!.

A test similar to the the following in macros.rs should reproduce the error:

#[derive(FromRow)]
struct Foo {
    id: i32,
    #[sqlx(default)]
    name: Option<String>,
}

#[sqlx_macros::test]
async fn test_query_as_default() -> anyhow::Result<()> {
    let mut conn = new::<Postgres>().await?;

    let foo = sqlx::query_as!(Foo, "SELECT id \"id!\" from (VALUES (1)) foos(id)",)
        .fetch_one(&mut conn)
        .await?;

    println!("{:?}", foo);

    Ok(())
}
error[E0063]: missing field `name` in initializer of `Foo`

Should/Could this be supported in sqlx, either through #[sqlx(default)] or some other mechanism? I had a glance at quote_query_as, columns_to_rust and expand_with_data, but I couldn't really tell 😞

@mehcode
Copy link
Member

mehcode commented Mar 27, 2021

This is unfortunately not possible without some unstable rust features (and a rewrite of how the type-safe macros are implemented). See #514 to track from our side.

@mehcode mehcode closed this as completed Mar 27, 2021
@mehcode mehcode added the duplicate This issue or pull request already exists label Mar 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants