Adding a new value to an existing ENUM Type

前端 未结 18 1318
春和景丽
春和景丽 2020-12-04 04:52

I have a table column that uses an enum type. I wish to update that enum type to have an additional possible value. I don\'t want to delete any exi

18条回答
  •  心在旅途
    2020-12-04 05:23

    A possible solution is the following; precondition is, that there are not conflicts in the used enum values. (e.g. when removing an enum value, be sure that this value is not used anymore.)

    -- rename the old enum
    alter type my_enum rename to my_enum__;
    -- create the new enum
    create type my_enum as enum ('value1', 'value2', 'value3');
    
    -- alter all you enum columns
    alter table my_table
      alter column my_column type my_enum using my_column::text::my_enum;
    
    -- drop the old enum
    drop type my_enum__;
    

    Also in this way the column order will not be changed.

提交回复
热议问题