migration fail when table exists

I have a table messages that is created in an earlier migration file:
DO $$ BEGIN
CREATE TYPE "public"."result" AS ENUM('LEFT', 'RIGHT');
EXCEPTION
WHEN duplicate_object THEN null;
END $$;
--> statement-breakpoint
CREATE 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-breakpoint
CREATE 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-breakpoint
DO $$ 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-breakpoint
DO $$ 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 files were generated with drizzle-kit generate --config=drizzle.config.ts On a completely fresh DB, running 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'
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server