小编Jef*_*eff的帖子

如何在不锁定表的情况下向 Postgres 中的 ENUM 添加新值?

我尝试了两种方法。

方法 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)

sql postgresql enums denormalization

4
推荐指数
2
解决办法
2892
查看次数

标签 统计

denormalization ×1

enums ×1

postgresql ×1

sql ×1