我尝试了两种方法。
方法 1:使用添加的新值创建一个新的 ENUM 并就地切换数据类型:
-- Rename existing enum
ALTER TYPE animal_species RENAME TO animal_species_old;
-- Create new enum with new value
CREATE TYPE animal_species AS ENUM (
'dog',
'cat',
'elephant'
);
-- Update the column of Animals to use the new enum
ALTER TABLE "Animals" ALTER COLUMN species SET DATA TYPE animal_species USING species::text::animal_species;
DROP TYPE animal_species_old;
Run Code Online (Sandbox Code Playgroud)
方法二:使用临时列
-- Create new enum type with a new name (this will be the name of the enum from now on)
CREATE …Run Code Online (Sandbox Code Playgroud)