How to delete an enum type value in postgres?

前端 未结 9 2037
没有蜡笔的小新
没有蜡笔的小新 2020-12-04 08:02

How do I delete an enum type value that I created in postgresql?

create type admin_level1 as enum(\'classifier\', \'moderator\', \'god\');

9条回答
  •  借酒劲吻你
    2020-12-04 08:42

    Very well written here:

    http://blog.yo1.dog/updating-enum-values-in-postgresql-the-safe-and-easy-way/

    rename the existing type

    ALTER TYPE status_enum RENAME TO status_enum_old;
    

    create the new type

    CREATE TYPE status_enum AS ENUM('queued', 'running', 'done');
    

    update the columns to use the new type

    ALTER TABLE job ALTER COLUMN job_status TYPE status_enum USING job_status::text::status_enum;
    

    remove the old type

    DROP TYPE status_enum_old;
    

    Possible Errors and Troubleshooting:

    • invalid input value for enum {enum name}: "{some value}" - One or more rows have a value ("{some value}") that is not in your new type. You must handle these rows before you can update the column type.
    • default for column "{column_name}" cannot be cast automatically to type {enum_name} - The default value for the column is not in your new type. You must change or remove the default value for the column before you can update the column type. Thanks to Philipp for this addition.

提交回复
热议问题