简体   繁体   English

带有来自字符串的枚举的 Postgres 更新列

[英]Postgres Update column with Enum from string

I am building a migration.我正在建立一个迁移。 Changed a column from string to Enum-Type and want to insert the saved values as Enum into the table, but no idea how to do this.将一列从字符串更改为枚举类型,并希望将保存的值作为枚举插入表中,但不知道如何执行此操作。 I inserted my values into a new table, droped the colum, created it with an enum type and got only the strings in the new table.我将我的值插入到一个新表中,删除列,使用枚举类型创建它并仅获取新表中的字符串。 The string have the same text as the enum.该字符串与枚举具有相同的文本。 The code is:代码是:

UPDATE event_participations
    SET kind = participations_migration.kind
    FROM participations_migration
    WHERE participations_migration.part_id = event_participations.id

Participations_type is the name of the enum with values like "going", "not_going". Participations_type 是枚举的名称,其值为“going”、“not_going”。 Now I have strings in participations_migration.kind and want to get the enum value to update the event_participations.kind.现在我在 participations_migration.kind 中有字符串并且想要获取枚举值来更新 event_participations.kind。

I need a subselect or join to update event_participations with the enum value according to the string which is set in participations_migration.kind.我需要一个子选择或连接来根据在 participations_migration.kind 中设置的字符串使用枚举值更新 event_participations。 Any idea?任何的想法?

If the text is identical to the enum member text, a cast to the enum type should be enough.如果文本与枚举成员文本相同,则转换为枚举类型就足够了。

    SET kind = participations_migration.kind::participations_type

A few years later - but I found the above doesn't work.几年后 - 但我发现上面的方法不起作用。 Using postgres 13.5 and datagrip to query, you must add quotations around the enum like so:使用 postgres 13.5 和 datagrip 进行查询,您必须在枚举周围添加引号,如下所示:

SET kind = participations_migration.kind::"participations_type"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM