Skip to content

When using "select_as" in the attribute macro, the column is not found #2919

@gregoryholder

Description

@gregoryholder

Description

When using "select_as" in the attribute macro, the column is no longer found.

Steps to Reproduce

With an entity model such as:

#[sea_orm::model]
#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "POINT_ARRET")]
pub struct Model {
    #[sea_orm(column_name = "ID", primary_key)]
    pub id: i64,
    #[sea_orm(select_as = "real", column_name = "X", column_type = "Double")]
    pub x: f64,
    #[sea_orm(select_as = "real", column_name = "Y", column_type = "Double")]
    pub y: f64,
}

(unfortunately the db I'm using requires a cast because the columns are a mix of ints and reals, thanks sqlite)

The request fails to find values for X and Y:

let pa = PointArret::find().one(&db).await;
dbg!(pa);
[src/main.rs:71:5] pa = Err(
    Query(
        SqlxError(
            ColumnNotFound(
                "X",
            ),
        ),
    ),
)

Performing the request manually and fetching the columns by index still works:

let query_raw = &db.query_one_raw(PointArret::find().build(DbBackend::Sqlite)).await.unwrap().unwrap();
dbg!(query_raw.column_names());
dbg!(query_raw.try_get_by_index::<Option<f64>>(1));
dbg!(query_raw.try_get_by_index::<Option<f64>>(2));
[src/main.rs:62:5] query_raw.try_get_by_index::<Option<f64>>(1) = Ok(
    Some(
        500812.85,
    ),
)
[src/main.rs:63:5] query_raw.try_get_by_index::<Option<f64>>(2) = Ok(
    Some(
        117632.84,
    ),
)

The constructed query is the following:

let string_query = PointArret::find().build(DbBackend::Sqlite).to_string();
dbg!(string_query);
[src/main.rs:58:5] string_query = "SELECT \"POINT_ARRET\".\"ID\", CAST(\"POINT_ARRET\".\"X\" AS real), CAST(\"POINT_ARRET\".\"Y\" AS real) FROM \"POINT_ARRET\""

I may be missing a column rename or something to make the result findable, but I wasn't able to find a solution.

Expected Behavior

Actual Behavior

Reproduces How Often

Workarounds

Versions

├── sea-orm v2.0.0-rc.28
│   ├── sea-orm-macros v2.0.0-rc.28 (proc-macro)
│   │   ├── sea-bae v0.2.1 (proc-macro)
│   ├── sea-query v1.0.0-rc.29
│   │   ├── sea-query-derive v1.0.0-rc.11 (proc-macro)
│   ├── sea-query-sqlx v0.8.0-rc.11
│   │   ├── sea-query v1.0.0-rc.29 (*)
│   ├── sea-schema v0.17.0-rc.17
│   │   ├── sea-query v1.0.0-rc.29 (*)
│   │   ├── sea-query-sqlx v0.8.0-rc.11 (*)
│   │   ├── sea-schema-derive v0.3.0 (proc-macro)

Using sqlx-sqlite backend, running on Ubuntu 24.04.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions