Skip to content

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

@OriolMunoz

Description

@OriolMunoz

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 😞

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateThis issue or pull request already exists

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions