当文本值不匹配时,Postgres将文本转换为枚举
我正在尝试将文本值转换为文本值不匹配新枚举的枚举值。 Postgres是否可以不必先删除列或在之前写一堆更新脚本?
CREATE TABLE "test_table" (
"id" uuid NOT NULL DEFAULT uuid_generate_v4(),
"shape" text
);
insert into test_table(shape) values ('Round');
insert into test_table(shape) values ('Square');
CREATE TYPE "public"."test_table_shape_enum" AS ENUM(
'round',
'square'
);
ALTER TABLE test_table
ALTER shape TYPE test_table_shape_enum USING shape::test_table_shape_enum;
[22P02] ERROR: invalid input value for enum test_table_shape_enum: "Round"
我看到这样做的几种方法是在进行Alter之前对所有行进行更新。
update test_table set shape='round' where shape='Round';
update test_table set shape='square' where shape='Square';
但是,如果我能做到这一点,那会很好。是否有更好的方法可以在运行Alter命令之前不必编写一堆更新脚本?
I'm trying to convert a column from text to enum value where the text values don't match the new enums. Is this possible with Postgres without having to drop the column first or write a bunch of update scripts prior?
CREATE TABLE "test_table" (
"id" uuid NOT NULL DEFAULT uuid_generate_v4(),
"shape" text
);
insert into test_table(shape) values ('Round');
insert into test_table(shape) values ('Square');
CREATE TYPE "public"."test_table_shape_enum" AS ENUM(
'round',
'square'
);
ALTER TABLE test_table
ALTER shape TYPE test_table_shape_enum USING shape::test_table_shape_enum;
[22P02] ERROR: invalid input value for enum test_table_shape_enum: "Round"
A few ways I see doing this is either do an update to all rows prior to doing the alter.
update test_table set shape='round' where shape='Round';
update test_table set shape='square' where shape='Square';
But it would be nice if I can do it with all one go; is there a better way of doing this without having to write a bunch of update scripts prior to running the alter command?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果唯一的问题是字母案例,请使用
lower()
:在 db<> fiddle。
If the only problem is the letter case, use
lower()
:Test it in Db<>Fiddle.