2

I currently have a table called employees having column access_level of type enum (accessLevel). But now I want to provide the employee multiple access_levels so now I need to convert the datatype from enum to array of enum.

I ran the following query

ALTER TABLE employees ALTER COLUMN access_level TYPE accessLevel[] USING access_level::accessLevel[]

But in response getting the following error
ERROR: cannot cast type accessLevel to accessLevel[] LINE 1: ... access_level TYPE accessLevel[] USING access_level::accessLev...

2
  • 2
    A properly normalized model without an enum and classic one-to-many relationship might be a better solution in the long run Commented Aug 25, 2022 at 7:25
  • @a_horse_with_no_name, I initially started with the same approach but then I realised this will end up creating too many rows so I stick to this methodology. It'll be great if you can help me understand the downsides of this approach Commented Aug 25, 2022 at 7:42

1 Answer 1

4

You can't cast a single value to an array, you need to build a new array:

ALTER TABLE employees 
    ALTER COLUMN access_level TYPE accessLevel[] 
    USING array[access_level];
Sign up to request clarification or add additional context in comments.

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.