为什么我不能将列从一个枚举转换为另一个枚举?



给定此设置

create type myenum_one as enum('foo', 'bar', 'baz');
create table mytable (
myvalue myenum_one
);
alter table mytable
add constraint mycheckconstraint check (myvalue != 'bar');
insert into mytable values ('foo');
create type myenum_two as enum('foo', 'bar');

然后在尝试更改列类型时失败

alter table mytable
alter column myvalue type myenum_two using myvalue::text::myenum_two;

错误

错误:运算符不存在:enum_two<gt;enum_one
提示:没有与给定名称和参数类型匹配的运算符。您可能需要添加显式类型强制转换。

您需要在更改列类型之前删除检查约束,然后重新创建它(如果它仍然相关(,就像一样

alter table mytable
drop constraint mycheckconstraint;
alter table mytable
alter column myvalue type myenum_two using myvalue::text::myenum_two;
alter table mytable
add constraint mycheckconstraint check (myvalue != 'bar');

最新更新