SeaQL / SeaQL/sea-orm

Alter enum column with new type (missing USING)

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

Nobody has claimed this yet.

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

Description

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

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 at the migration using alter_table, modify_column, and ColumnDef, then trace the PostgreSQL SQL generation for the reported enum-column change. Verify how the generated ALTER TABLE statement could represent the requested USING expression and confirm that the resulting SQL supports converting existing enum data.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, rust
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.