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

发布于 2025-01-20 02:57:26 字数 637 浏览 2 评论 0原文

鉴于此设置,

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 <>枚举_one
提示:没有运算符与给定名称和参数类型匹配。您可能需要添加显式类型转换。

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
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生来就爱笑 2025-01-27 02:57:26

您需要在更改列类型之前删除支票约束,然后在此之后重新创建(如果仍然相关),例如

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');

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');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文