Skip to content

Type override together with wildcard does not work #4151

@SamoFMF

Description

@SamoFMF

I have found these related issues/pull requests

None directly for this issue.

Description

Overriding the type for a subset of columns, as shown in Compile-time verification example from docs, does not work with cargo sqlx prepare.

Reproduction steps

CREATE TABLE users
(
    id BIGINT NOT NULL PRIMARY KEY,
    name TEXT NOT NULL
);
#[derive(sqlx::Type)]
#[sqlx(transparent)]
struct UserId(i64);

struct MyUser { id: UserId, name: String }

async fn sql(conn: &mut sqlx::PgConnection) {
    // fetch all properties from user and override the type in Rust for `id`
    let user = sqlx::query_as!(MyUser, r#"SELECT users.*, id as "id: UserId" FROM users"#)
        .fetch_one(conn).await.unwrap();
}
$ cargo sqlx prepare
    Checking sqlx-test v0.1.0 (/sqlx-test)
error[E0062]: field `id` specified more than once
 --> src/main.rs:9:16
  |
9 |     let user = sqlx::query_as!(MyUser, r#"SELECT users.*, id as "id: UserId" FROM users"#)
  |                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |                |
  |                used more than once
  |                first use of `id`
  |
  = note: this error originates in the macro `$crate::sqlx_macros::expand_query` which comes from the expansion of the macro `sqlx::query_as` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0062`.
error: could not compile `sqlx-test` (bin "sqlx-test") due to 1 previous error
error: `cargo check` failed with status: exit status: 101

SQLx version

0.8.6

Enabled SQLx features

runtime-tokio, postgres

Database server and version

Postgres 16.3

Operating system

Ubuntu 24.04

Rust version

1.93.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions