nodejsdeveloperskh

create-table-alter-enum-column-to-array-of-that-enum

Jan 19th, 2022 (edited)
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- CreateEnum
  2. CREATE TYPE "Hobby" AS ENUM ('ENTREPRENEUR', 'SPORT', 'MUSIC', 'MOVIE', 'CODING', 'TRAVELING', 'WORKING', 'LEARNING');
  3.  
  4. -- CreateTable
  5. CREATE TABLE "tasks" (
  6.     "id" SERIAL NOT NULL,
  7.     "firstname" VARCHAR(100) NOT NULL,
  8.     "imageId" VARCHAR(36) NOT NULL,
  9.     "hobbies" "Hobby" NOT NULL DEFAULT E'LEARNING',
  10.     "favoriteNumbers" INTEGER[],
  11.  
  12.     CONSTRAINT "tasks_pkey" PRIMARY KEY ("id")
  13. );
  14.  
  15. -- But this migration fails because I have records that their hobbies is full
  16. -- AlterTable
  17. ALTER TABLE "tasks" ALTER COLUMN "hobbies" DROP DEFAULT,
  18. ALTER COLUMN "hobbies" SET DATA TYPE "Hobby"[];
  19.  
  20. -- You have to use USING:
  21. ALTER TABLE "tasks" ALTER COLUMN "hobbies" DROP DEFAULT,
  22. ALTER COLUMN "hobbies" SET DATA TYPE "Hobby"[] USING ARRAY["hobbies"];
Add Comment
Please, Sign In to add comment