error: ALTER TYPE … ADD cannot run inside a transaction block

后端 未结 4 1382
温柔的废话
温柔的废话 2021-02-12 11:22

I am trying to add new type value to my existing types in PostgreSQL. But I get the following error

error: ALTER TYPE ... ADD cannot run inside a transact

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-12 12:09

    As it was mentioned above you can't edit enum within transaction block. But you can create the new one. Here are the steps:

    1. Change type from request_type to varchar for all columns/tables which use this type:
    ALTER TABLE table_name ALTER COLUMN request_type TYPE VARCHAR(255);
    
    1. Drop and create again request_type enum:
    DROP TYPE IF EXISTS request_type;
    CREATE TYPE request_type AS ENUM ('OLD_VALUE_1', 'OLD_VALUE_2', 'NEW_VALUE_1', 'NEW_VALUE_2'); 
    
    1. Revert type from varchar to request_type for all columns/tables (revert step one):
    ALTER TABLE table_name ALTER COLUMN request_type TYPE request_type USING (request_type::request_type);
    

提交回复
热议问题