[BUG]: pgEnum -> type "xxx" already exists during migration
Problem
What version of `drizzle-orm` are you using? "^0.35.3" What version of `drizzle-kit` are you using? "^0.26.2" Describe the Bug Hi In a nextjs app with "drizzle-orm/pg-core" i have this enum [code block] And when I migrate my schemas first time all is ok, but after a new migration i got the error โ Migration failed PostgresError: type "xxx" already exists at ErrorResponse (D:\Work\webapps\damhub\node_modules\postgres\cjs\src\connection.js:788:26) at handle (D:\Work\webapps\damhub\node_modules\postgres\cjs\src\connection.js:474:6) at Socket.data (D:\Work\webapps\damhub\node_modules\postgres\cjs\src\connection.js:315:9) at Socket.emit (node:events:519:28) at addChunk (node:internal/streams/readable:559:12) at readableAddChunkPushByteMode (node:internal/streams/readable:510:3) at Readable.push (node:internal/streams/readable:390:5) If I do a db: push all works fine. regards Expected behavior I expected a success migration after new migration. Not only the first time Environment & setup This is my package { "name": "xxx", "version": "0.1.0", "private": true, "scripts": { "dev": "next dev --turbo", "build": "next build", "start": "next start", "lint": "next lint", "db:generate": "drizzle-kit generate", "db:migrate": "tsx lib/db/migrate.ts", "db:drop": "drizzle-kit drop", "db:pull": "drizzle-kit introspect", "db:push": "drizzle-kit push", "db:studio": "drizzle-kit studio", "db:check": "d
Error Output
error โ Migration failed
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Fix Enum Type Already Exists Error During Migration
The error occurs because PostgreSQL does not allow creating an enum type that already exists. When running migrations, if an enum type is defined in the schema and the migration attempts to create it again without checking for its existence, it leads to this error.
Awaiting Verification
Be the first to verify this fix
- 1
Check for Existing Enum Type
Before attempting to create the enum type in your migration file, check if it already exists. This can be done by querying the PostgreSQL system catalog.
typescriptconst existingEnum = await db.query('SELECT 1 FROM pg_type WHERE typname = $1', ['your_enum_name']); - 2
Modify Migration Script
Update your migration script to include a conditional check for the existence of the enum type before creating it. If it exists, skip the creation step.
typescriptif (!existingEnum.length) { await db.query('CREATE TYPE your_enum_name AS ENUM (''value1'', ''value2'');'); } - 3
Run Migration Again
After modifying the migration script, run the migration command again to ensure that it executes without errors.
bashnpm run db:migrate - 4
Test Migration
Test the migration process by creating a new migration that modifies the existing schema and verify that it completes successfully without the enum type error.
bashnpm run db:generate && npm run db:migrate
Validation
Confirm that the migration completes successfully without throwing the 'type already exists' error. Additionally, verify that the enum type is correctly created or remains unchanged in the database.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep