Skip to content

Improved detection of nullability for postgres #696

Open
@jyn514

Description

@jyn514

This query currently returns an Option<i64>. It would be nice to instead return i64, because COUNT is never nullable.

async fn f() -> Result<i64, sqlx::Error> {
    sqlx::query!(
        "SELECT COUNT(*) FROM crates",
    )
    .fetch_one(unimplemented!())
    .await
    .map(|rec| rec.count)
}
error[E0308]: mismatched types
  --> src/main.rs:6:5
   |
6  | /     sqlx::query!(
7  | |         "SELECT COUNT(*) FROM crates",
8  | |     )
9  | |     .fetch_one(unimplemented!())
10 | |     .await
11 | |     .map(|rec| rec.count)
   | |_________________________^ expected `i64`, found enum `std::option::Option`
   |
   = note: expected enum `std::result::Result<i64, _>`
              found enum `std::result::Result<std::option::Option<i64>, _>`

@mehcode mentioned that I can give SQLx a type hint by using r#"SELECT COUNT(*) as "count!" FROM crates;"#; this is a good workaround in the meantime but I'd love for sqlx to be able to figure it out itself. A possible implementation is to select from a temporary table or view and get the nullability from that view.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions