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

Returning model with enum field with find_by_statement #2423

Open
qRoC opened this issue Nov 20, 2024 · 0 comments
Open

Returning model with enum field with find_by_statement #2423

qRoC opened this issue Nov 20, 2024 · 0 comments

Comments

@qRoC
Copy link

qRoC commented Nov 20, 2024

database: postgres

Enum:

#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum)]
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "test_status")]
pub enum TestStatus {
    #[sea_orm(string_value = "new")]
    New,
    #[sea_orm(string_value = "processing")]
    Processing,
}

Model:

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "test")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i64,
    pub status: Status,
    pub created_at: DateTime,
}

Query:

    let select_query = Query::select()
        .column(test::Column::Id)
        .from(test::Entity)
        .and_where(test::Column::Status.eq(TestStatus::New))
        .limit(1)
        .lock_with_behavior(LockType::Update, LockBehavior::SkipLocked)
        .to_owned();

    let update_query = Query::update()
        .table(test::Entity)
        .value(test::Column::Status, TestStatus::Processing.as_enum())
        .and_where(test::Column::Id.in_subquery(select_query))
        .returning_all()
        .to_owned();

    let stmt = db.get_database_backend().build(&update_query);

    test::Model::find_by_statement(stmt).one(db).await

Query is executed, but final result is:

Query Error: error occurred while decoding column "status": mismatched types; Rust type `core::option::Option<alloc::string::String>` (as SQL type `TEXT`) is not compatible with SQL type `test_status`
@qRoC qRoC changed the title Returning model with enum field Returning model with enum field with find_by_statement Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant