简体   繁体   English

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

[英]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.

相关问题 如何将枚举值从一个枚举列“复制”到另一个枚举列? - How do I “copy” enum values from one enum column to another enum column? 是否可以在 PostgreSQL 中从一个枚举转换为另一个枚举 - Is it possible to cast from one enum to another in PostgreSQL 如何将数据从一个 PostgreSQL 数据库迁移到另一个(表/列名略有不同)? - How can I migrate data from one PostgreSQL database to another (with slightly different table/column names)? 如何在一个表中使用另一个表中的FK填充我的FK列? - How can I populate my FK column in one table with the FK from another table? 如何在Postgresql数据库中创建具有一列枚举数据类型的表? - How can i create a table with one column of enum datatype in postgresql database? 如何在 Supabase 中创建枚举列? - How can I create an enum column in Supabase? 我可以将 Flask Sqlalchemy 中一列的 json 数据与另一列的数据进行比较吗? - Can I compare the json data of one column to another in Flask Sqlalchemy? 无法将PostgreSQL表列从varchar类型转换为int - Can't convert postgresql table column from type varchar to int 将 postgres 列类型从枚举转换为枚举数组 - Convert postgres column type from enum to array of enums 是否可以将枚举类型从一个模式复制到另一个模式 - Is it possible to copy an enum type from one schema to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM