DO $$ BEGIN CREATE TYPE "public"."result" AS ENUM('LEFT', 'RIGHT');EXCEPTION WHEN duplicate_object THEN null;END $$;--> statement-breakpointCREATE TABLE IF NOT EXISTS "messages" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "message" text NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL);...
DO $$ BEGIN CREATE TYPE "public"."result" AS ENUM('LEFT', 'RIGHT');EXCEPTION WHEN duplicate_object THEN null;END $$;--> statement-breakpointCREATE TABLE IF NOT EXISTS "messages" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "message" text NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL);...
In a subsequent migration, I add a column to this table:
ALTER TABLE "messages" ADD COLUMN "email_id" uuid;--> statement-breakpointDO $$ BEGIN ALTER TABLE "messages" ADD CONSTRAINT "messages_email_id_emails_id_fk" FOREIGN KEY ("email_id") REFERENCES "public"."emails"("id") ON DELETE no action ON UPDATE no action;EXCEPTION WHEN duplicate_object THEN null;END $$;
ALTER TABLE "messages" ADD COLUMN "email_id" uuid;--> statement-breakpointDO $$ BEGIN ALTER TABLE "messages" ADD CONSTRAINT "messages_email_id_emails_id_fk" FOREIGN KEY ("email_id") REFERENCES "public"."emails"("id") ON DELETE no action ON UPDATE no action;EXCEPTION WHEN duplicate_object THEN null;END $$;
Both of these
.sql
.sql
files were generated with
drizzle-kit generate --config=drizzle.config.ts
drizzle-kit generate --config=drizzle.config.ts
On a completely fresh DB, running
drizzle-kit migrate --config=drizzle.config.ts
drizzle-kit migrate --config=drizzle.config.ts
gets an error:
[⣯] applying migrations...error: relation "messages" does not exist at /Users/pthieu/www/cedar-service/node_modules/drizzle-kit/bin.cjs:77696:15 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at <anonymous> (/Users/pthieu/www/cedar-service/node_modules/src/pg-core/dialect.ts:89:7) at NodePgSession.transaction (/Users/pthieu/www/cedar-service/node_modules/src/node-postgres/session.ts:155:19) at PgDialect.migrate (/Users/pthieu/www/cedar-service/node_modules/src/pg-core/dialect.ts:82:3) at migrate (/Users/pthieu/www/cedar-service/node_modules/src/node-postgres/migrator.ts:10:2) { length: 106, severity: 'ERROR', code: '42P01',... routine: 'RangeVarGetRelidExtended'
[⣯] applying migrations...error: relation "messages" does not exist at /Users/pthieu/www/cedar-service/node_modules/drizzle-kit/bin.cjs:77696:15 at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at <anonymous> (/Users/pthieu/www/cedar-service/node_modules/src/pg-core/dialect.ts:89:7) at NodePgSession.transaction (/Users/pthieu/www/cedar-service/node_modules/src/node-postgres/session.ts:155:19) at PgDialect.migrate (/Users/pthieu/www/cedar-service/node_modules/src/pg-core/dialect.ts:82:3) at migrate (/Users/pthieu/www/cedar-service/node_modules/src/node-postgres/migrator.ts:10:2) { length: 106, severity: 'ERROR', code: '42P01',... routine: 'RangeVarGetRelidExtended'