Prisma migration fails with "relation already exists" after partial run
Problem
A Prisma migration fails mid-way through execution (e.g. due to a constraint violation or connection drop). When you attempt to run the migration again, it fails with 'relation already exists' because some tables were created before the failure. The Prisma migrations table marks the migration as 'failed' but does not roll back the partial changes, leaving the database and migration history out of sync.
Error Output
ERROR: relation "User" already exists Migration failed to apply cleanly to the shadow database. Error code: P3006
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Mark failed migration as rolled-back, clean up partial tables, and re-run
Prisma does not automatically roll back failed migrations. The _prisma_migrations table marks it as 'failed' but partial DDL changes remain in the database.
Trust Score
7 verifications
- 1
Check migration status
See which migration failed:
bashnpx prisma migrate status - 2
Mark as rolled back
Tell Prisma to ignore the partial run:
bashnpx prisma migrate resolve --rolled-back "20240101_migration_name" - 3
Drop partially created tables
Connect directly to the database and drop any tables/columns created by the failed migration:
sqlDROP TABLE IF EXISTS "NewTable" CASCADE; - 4
Re-run the migration
Run migrate deploy again:
bashnpx prisma migrate deploy
Validation
prisma migrate status shows all migrations as Applied with no failed entries.
Verification Summary
Sign in to verify this fix
Environment
- Product
- Prisma
- Version
- 5.x
- Environment
- production
Submitted by
Alex Chen
2450 rep