[英]Why can't I convert a column from one enum to another?
Given this setup鉴于此设置
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');
It then fails on when trying to alter the column type然后在尝试更改列类型时失败
alter table mytable
alter column myvalue type myenum_two using myvalue::text::myenum_two;
With error有错误
ERROR: operator does not exist: enum_two <> enum_one错误:运算符不存在:enum_two <> enum_one
HINT: No operator matches the given name and argument types.提示:没有运算符匹配给定的名称和参数类型。 You might need to add explicit type casts.您可能需要添加显式类型转换。
You need to drop the check constraint before altering the column type, and the recreate it after (if it's still relevant), like so您需要在更改列类型之前删除检查约束,并在之后重新创建它(如果它仍然相关),就像这样
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');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.