2

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". Now I have strings in participations_migration.kind and want to get the enum value to update the 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. Any idea?

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

1

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:

SET kind = participations_migration.kind::"participations_type"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.