Sample Header Ad - 728x90

Casting an array of texts to an array of UUIDs

19 votes
4 answers
61689 views
How can I cast an array of texts into an array of UUIDs? I need to do a join between two tables: users and projects. The users table has an array field named project_ids containing the project IDs as text. The projects table had a UUID field named id. My initial idea was a query looks like: SELECT * FROM projects JOIN users ON projects.id = ANY(users.project_ids) But that does not work since users.project_ids are not UUIDs so I tried: projects.id = ANY(users.project_ids::uuid[]) and even: projects.id = ANY(ARRAY[users.project_ids]::uuid[]) but neither one works: > ERROR: invalid input syntax for type uuid: "" UPDATE @a_horse_with_no_name is definitely right. The best option should be using an array of UUIDs. The question now is how can I alter an array of text into an array of uuid? The users table is currently empty (0 records). I have tried ALTER TABLE "users" ALTER COLUMN "project_ids" SET DATA TYPE UUID USING "project_ids"::uuid[]; which generates ERROR: result of USING clause for column "product_ids" cannot be cast automatically to type uuid HINT: You might need to add an explicit cast. > ALTER TABLE "users" ALTER COLUMN "product_ids" SET DATA TYPE UUID > USING "product_ids"::UUID; I have also tried ALTER TABLE "users" ALTER COLUMN "project_ids" SET DATA TYPE UUID[] USING "project_ids"::uuid[]; which generates > ERROR: default for column "project_ids" cannot be cast automatically > to type uuid[] The column is set to an empty array as default. I'm running PG version 10.4 and project_ids is currently text[] nullable.
Asked by Sig (455 rep)
Nov 8, 2018, 07:52 AM
Last activity: Nov 27, 2023, 11:11 AM