sea-orm icon indicating copy to clipboard operation
sea-orm copied to clipboard

In 1.1.7, DerivePartialModel doesn't cast enums to TEXT causing mismatched types errors

Open raimundomartins opened this issue 7 months ago • 0 comments
trafficstars

Description

Starting from v1.1.7, sea_orm::executor::select::Select::into_partial_model() requires that all enum columns be annotated with a cast_as or receive an Err when retrieving data. This was not needed in 1.1.6.

Steps to Reproduce

  1. Have an enum in the DB schema (I used Postgres backend)
  2. Make a partial model using #[derive(DerivePartialModel)] that has a field corresponding to a column of that enum type.
  3. Call Selector::into_partial_model().one(&db_connection)

Expected Behavior

Returns an Ok() with the data from DB.

Actual Behavior

Returns an Err:

Database(Query(SqlxError(ColumnDecode { index: "\"some_col\"", source: "mismatched types; Rust type `core::option::Option<alloc::string::String>` (as SQL type `TEXT`) is not compatible with SQL type `some_enum`" })))

Reproduces How Often

Always.

Workarounds

Annotate the struct with a cast_as(Alias::new(\"TEXT\"))

Reproducible Example

#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize, Deserialize)]
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "some_postgres_enum")]
pub enum SomePostgresEnum {
	#[sea_orm(string_value = "val1")]
	Val1,
	#[sea_orm(string_value = "val2")]
	Val2,
}

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[sea_orm(table_name = "asset")]
pub struct Asset {
	#[sea_orm(primary_key, auto_increment = false)]
	pub id: String,
	pub some_col: SomePostgresEnum,
        pub other_field: String,
}

#[derive(Clone, Debug, FromQueryResult, DerivePartialModel)]
#[sea_orm(entity = "Asset")]
pub struct DbPartialAsset {
	pub id: String,
	// The following line is only needed for seaorm =1.1.7
	#[sea_orm(from_expr = "Expr::col(asset::Column::SomeCol).cast_as(Alias::new(\"TEXT\"))")]
	pub some_col: SomePostgresEnum,
}

#[tokio::main]
async fn main() {
    let db = Database::connect("xxxx").await.unwrap();

    let result = entity::Asset::find()
        .into_partial_model::<DbPartialAsset>()
        .all(&db).await;
}

Versions

│ ├── sea-orm v1.1.7 │ │ ├── sea-orm-macros v1.1.7 (proc-macro) │ │ │ ├── sea-bae v0.2.1 (proc-macro) │ │ ├── sea-query v0.32.3 │ │ │ ├── sea-query-derive v0.4.3 (proc-macro) │ │ ├── sea-query-binder v0.7.0 │ │ │ ├── sea-query v0.32.3 (*)

PostgreSQL 14.17 Ubuntu 14.17-0ubuntu0.22.04.1 (Pop_OS!)

(I just confirmed with 1.1.8, which I only realized existed after creating the issue, and it still happens)

raimundomartins avatar Mar 31 '25 11:03 raimundomartins