Fix Prisma database migration failures
Prisma migration errors occur when the migration SQL can't be applied to your database. This happens due to conflicting migrations, data that violates new constraints, or schema drift between your Prisma schema and the actual database.
Error messages you might see
Error: P3009 migrate found failed migrations in the target databaseError: P3006 Migration failed to apply cleanly to the shadow database.Why this happens in AI-generated code
Migration conflicts from AI-generated schema changes
AI tools modify the Prisma schema without considering existing migrations, creating conflicts when new migrations try to alter columns or tables that have already been changed.
Existing data violates new constraints
AI-generated schema changes add NOT NULL constraints or unique indexes to columns that already contain null or duplicate values in the database.
Shadow database connection issues
AI-generated database URLs point to managed databases that don't allow shadow database creation (required by prisma migrate dev), causing migration commands to fail.
How to fix it
Reset migrations in development
In development, run `npx prisma migrate reset` to drop and recreate the database with all migrations applied fresh. Never do this in production - use `prisma migrate deploy` instead.
Handle data migration for constraint changes
Before adding NOT NULL or unique constraints, create a migration that cleans up existing data first (fill nulls with defaults, remove duplicates), then apply the constraint.
Get professional help
Still stuck? Our engineers can resolve complex migration issues and schema drift. Visit /products to get started.
Can't fix it yourself?
Our code audit identifies this issue and dozens more. Get a prioritized fix list.
Security Review
Automated Security Scan
AI-powered analysis of your codebase. Get a detailed report with prioritized findings within 24 hours.
Get StartedSecurity Review
Manual Security Review
Expert engineer works on your project directly. Fixed scope, fixed price, no surprises.
Get a QuoteSecurity Review
Full Pentest
Enterprise-grade engagement tailored to your needs. Dedicated engineer, ongoing support.
Fix Bugs
Code Audit
AI-powered analysis of your codebase. Get a detailed report with prioritized findings within 24 hours.
Get StartedFix Bugs
Bug Fixing
Expert engineer works on your project directly. Fixed scope, fixed price, no surprises.
Get a QuoteFix Bugs
Ongoing Support
Enterprise-grade engagement tailored to your needs. Dedicated engineer, ongoing support.
Refactor Code
Code Audit
AI-powered analysis of your codebase. Get a detailed report with prioritized findings within 24 hours.
Get StartedRefactor Code
Refactoring
Expert engineer works on your project directly. Fixed scope, fixed price, no surprises.
Get a QuoteRefactor Code
Full Rewrite
Enterprise-grade engagement tailored to your needs. Dedicated engineer, ongoing support.
100% of your audit purchase is credited toward any paid service. Start with an audit, then let us fix what we find.
Frequently asked questions
Can I edit a migration file after it's been created?
Only if it hasn't been applied to any database yet. Once applied, Prisma tracks the migration checksum. Editing an applied migration will cause a checksum mismatch error. Create a new migration instead.
How do I handle migrations in production?
Use `prisma migrate deploy` (not `prisma migrate dev`) in production. It applies pending migrations without generating new ones or using a shadow database. Always back up your database before migrating.
Related resources
Still stuck? We can fix it for you.
Send us your repo. We'll diagnose the issue and give you a fixed quote within 24 hours.