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!(..)/query_as!(..) strange behavior when inferring types for queries #3736

Open
zshell31 opened this issue Feb 11, 2025 · 0 comments
Open
Labels

Comments

@zshell31
Copy link
Contributor

zshell31 commented Feb 11, 2025

I have found these related issues/pull requests

I found only issues related to inferring types for nullable fields

Description

Let's say I have the following table in Postgres:

CREATE TABLE test (
id varchar(64) PRIMARY KEY,
name text NOT NULL
);

Also let's say I have three query options:

async fn sql1(session: &mut Transaction<'_, Postgres>) -> sqlx::Result<()> {
    let rows = sqlx::query!(r#"SELECT id AS "id: i64", name AS "name: i64" FROM test"#)
        .fetch_all(&mut **session)
        .await?;

    Ok(())
}

async fn sql2(session: &mut Transaction<'_, Postgres>) -> sqlx::Result<()> {
    struct Row {
        id: i64,
        name: i64,
    }
    let rows = sqlx::query_as!(Row, r#"SELECT id, name FROM test"#)
        .fetch_all(&mut **session)
        .await?;

    Ok(())
}

async fn sql3(session: &mut Transaction<'_, Postgres>) -> sqlx::Result<()> {
    struct Row {
        id: i64,
        name: i64,
    }

    let rows = sqlx::query_as!(
        Row,
        r#"SELECT id AS "id: i64", name AS "name: i64" FROM test"#
    )
    .fetch_all(&mut **session)
    .await?;

    Ok(())
}

Why does only option 2 fail?

  --> src/main.rs:16:16
   |
16 |     let rows = sqlx::query_as!(Row, r#"SELECT id, name FROM test"#)
   |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<std::string::String>` is not implemented for `i64`, which is required by `std::string::String: Into<_>`
   |

At the same time, in documentation the following is written:

If you select a column foo as "foo: _" (Postgres/SQLite) or foo as `foo: _` (MySQL) it causes that column to be inferred based on the type of the corresponding field in the given record struct. Runtime type-checking is still done so an error will be emitted if the types are not compatible.

Reproduction steps

See code snippet in description

SQLx version

0.8.3

Enabled SQLx features

"runtime-tokio-rustls", "postgres"

Database server and version

Postgres 15.7

Operating system

Ubuntu 20.04

Rust version

1.84.1

@zshell31 zshell31 added the bug label Feb 11, 2025
@zshell31 zshell31 changed the title Strange behavior when inferring types for queries query!(..)/query_as!(..) strange behavior when inferring types for queries Feb 11, 2025
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

1 participant