SeaQL / SeaQL/sea-orm

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

Open
#2,919 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
9.9k
Forks
734
Avg merge
6h 36m
Merged PRs (30d)
8

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the attribute macro handling of select_as and the generated result-column mapping, using the PointArret::find() SQLite reproduction shown in src/main.rs. Verify the generated query and named-column lookup; done means fetching the model succeeds for the cast X and Y columns.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust, sqlite
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.