-
Notifications
You must be signed in to change notification settings - Fork 29
Description
Problem
Changing a table column's type to a custom enum with pgschema is not reliable if the column is currently a text or varchar type, especially when a default value is present. There are two related migration failures:
Issue 1: If the column contains non-enum data and pgschema generates migration SQL like:
ALTER TABLE clientstatehistory ALTER COLUMN action TYPE action_type;PostgreSQL fails with:
ERROR: column "action" cannot be cast automatically to type action_type (SQLSTATE 42804)
This fails because PostgreSQL requires a USING clause (e. g., USING action::action_type) to specify how existing values should be cast when migrating to an enum type.
After fixing the issue above by hand I was introduced to a 2nd issue:
Issue 2: If the column has a default value (e.g., 'open' or 'pending' as text), even with a proper USING clause, PostgreSQL fails to cast the default automatically:
ERROR: default for column "action" cannot be cast automatically to type action_type (SQLSTATE 42804)
This requires dropping the default before the type change and re-setting it afterward with a valid enum value.