如何将枚举值从一个枚举列“复制”到另一个枚举列?

问题描述

我正在尝试复制表中两列之间的枚举值。这两种枚举类型具有相同的枚举值

UPDATE dogs SET breed = breed_old;
...
ERROR:  column "breed" is of type "breed" but expression is of type "breed_old"

我也试过:

UPDATE dogs SET breed = breed_old::text;
...
ERROR:  column "breed" is of type "breed" but expression is of type text

任何帮助将不胜感激。

解决方法

create type foo as enum ('a','b');
create type bar as enum ('a','b');

select 'a'::foo;
┌─────┐
│ foo │
├─────┤
│ a   │
└─────┘

select 'a'::foo::text;
┌──────┐
│ text │
├──────┤
│ a    │
└──────┘

select 'a'::foo::text::bar;
┌─────┐
│ bar │
├─────┤
│ a   │
└─────┘

或者可能更方便:

update dogs set
    breed = case breed_old
        when 'val1' then 'val1'::breed
        when 'val2' then 'val2'::breed
        ...
    end;