我正在尝试修改架构中枚举的值(feature下面示例中的“”)。
我尝试通过重命名旧枚举并引入一个具有我想要的值的新枚举,然后将表定义更改为新枚举来实现此目的。
我在这里关注这篇博客文章:https://blog.yo1.dog/updating-enum-values-in-postgresql-the-safe-and-easy-way/。但我的列实际上是枚举的数组,而不是简单的枚举列。
当我尝试运行alter table以下语句中的语句时,出现错误:
[42804] 错误:列“features”的类型为 feature_old[],但表达式的类型为 feature_v2[] 提示:您需要重写或转换表达式。
alter type feature rename to feature_old;
create type feature_v2 as enum (
'enable_create_keyword',
'enable_make_payment',
'enable_test_data_flags'
);
-- ... cleanup of column array values to be compatible with new enum ...
alter table app_user alter column features type feature_v2
using features::feature_old[]::feature_v2[];
drop type feature_old;
Run Code Online (Sandbox Code Playgroud)
但是,我迷路了 - 强制转换表达式应该是什么样子?
Postgres 版本是 9.6
编辑
这是@VaoTsun 请求的feature枚举和表的先前版本的架构 DDL 的相关部分。app_user
-- feature enum and column
create type feature as enum ('enable_create_keyword', 'enable_make_payment');
comment on type feature is
'if default functionality is disabled feature name starts with enable_, if default is enabled starts with disable_'
;
alter table app_user add column
features feature[] not null default ARRAY[]::feature[];
-- feature data
update app_user
set features = ARRAY['enable_create_keyword', 'enable_make_payment']::feature[]
where email = 'test1@example.com';
update app_user
set features = ARRAY['enable_create_keyword']::feature[]
where email = 'test2@example.com';
Run Code Online (Sandbox Code Playgroud)
感谢 Vao Tsun 和 Nick Barnes;这是似乎对我有用的代码。我已将 Vao Tsun 的答案标记为正确。任何提供更简洁版本的答案将不胜感激。
alter type feature rename to feature_old;
create type feature_v2 as enum (
'enable_create_keyword',
'enable_make_payment',
'enable_test_data_flags'
);
alter table app_user alter column features drop default ;
alter table app_user alter column features type feature_v2[]
using features::feature_old[]::text[]::feature_v2[];
alter table app_user alter column features set default ARRAY[]::feature_v2[];
drop type feature_old;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2308 次 |
| 最近记录: |