SeaQL / sea-orm

🐚 An async & dynamic ORM for Rust
https://www.sea-ql.org/SeaORM/
Apache License 2.0
6.56k stars 459 forks source link

Alter enum column with new type (missing USING) #2231

Open zezic opened 1 month ago

zezic commented 1 month ago

Description

When trying to alter column of enum type to make it use the new enum type I'm getting a error from Postgres saying:

[1642] LOG:  execute sqlx_s_4: ALTER TABLE "fact" ALTER COLUMN "kind" TYPE fact_kind, ALTER COLUMN "kind" SET NOT NULL
[1642] ERROR:  column "kind" cannot be cast automatically to type fact_kind
[1642] HINT:  You might need to specify "USING kind::fact_kind".

The part of migration causing this error looks like that:

        manager
            .alter_table(
                Table::alter()
                    .table(m20240307_074314_create_fact_table::Fact::Table)
                    .modify_column(
                        ColumnDef::new(m20240307_074314_create_fact_table::Fact::Kind)
                            .enumeration(FactKindEnum, FactKind::iden_values())
                            .not_null(),
                    )
                    .to_owned(),
            )
            .await?;

Steps to Reproduce

  1. Using PostgreSQL as backend run migration to prepare table with some enum column.
  2. Using another migration try to alter that column and convert it to other (for example, extended version of previous) enum type.
  3. PostgreSQL will say that expression is missing the USING clause.

Expected Behavior

Expected ColumnDef to be able to generate statement which includes the extra USING clause followed with explicit column and its type – https://www.postgresql.org/docs/current/ddl-alter.html#DDL-ALTER-COLUMN-TYPE , or optionally followed by expression column_name::text::type_name to use thru-text cast.

Actual Behavior

The SQL appears to be generated without USING clause (PostgreSQL refuses to convert existing data to implicit type without it).

Reproduces How Often

Reproduced always.

Workarounds

Write raw SQL by hand.

Versions

sea-orm-migration 0.12.15