How to fix P3006: Migration failed to apply cleanly to the shadow database in Prisma
This error occurs when Prisma's shadow database (used for detecting schema drift) cannot apply your migrations cleanly. Common causes include schema drift, permission issues, manual database changes, or conflicting migrations. Resolve it by resetting your database, syncing your schema, or fixing permissions.
Quick fix
Fixes P3006: Migration failed to apply cleanly to the shadow database
npx prisma migrate dev- Check the detailed error message
- Check for manual database changes
- Verify your Prisma schema matches your migrations
- Run prisma db pull to re-baseline your schema
npx prisma migrate devP3006: Migration failed to apply cleanly to the shadow database
npx prisma migrate devWhat this error means
The P3006 error occurs when Prisma Migrate cannot apply your migration to the shadow database during development. The shadow database is a temporary, automatically-created database that Prisma uses to detect schema drift and ensure migrations are safe before applying them to your actual development database. When a migration fails to apply cleanly to this shadow database, it indicates a mismatch between your migration files, your Prisma schema, and your actual database state. This could mean your schema is out of sync with your migration history, permissions are insufficient, or the database was modified manually outside of Prisma Migrate.
Typical symptoms
- Error message "P3006: Migration {name} failed to apply cleanly to the shadow database" when running prisma migrate dev
- Migration works on your development database but fails on the shadow database
- The error mentions a specific SQL issue like "relation already exists" or "column does not exist"
- You switched between git branches and now migrations fail
- You manually edited the database outside of Prisma and now migrations cannot apply
- Permission errors like "permission denied" or "could not create trigger"
Common causes
- Database schema drift: Your development database was manually modified or has uncommitted manual schema changes that don't match your Prisma schema or migration files
- Manual migration edits: You edited a migration file after it was already applied to the database
- Database permission issues: The database user lacks permissions to create/drop tables, indexes, or triggers needed by the migration
- Switching git branches: Moving between branches with different migration histories can cause conflicts if migrations were partially applied
- Prisma schema out of sync: Your schema.prisma file does not match your migration files or database state
- Uncommitted changes in version control: Migration files exist in your database history but not in your current git branch
- Character encoding or syntax issues: The migration SQL has UTF-8 BOM encoding or invalid syntax for your database
How to fix "P3006: Migration failed to apply cleanly to the shadow database"
Run the migration command with verbose output to see exactly what failed:
npx prisma migrate devRead the full error message—it will tell you which table, column, or constraint is causing the issue. For example:
ERROR: relation "PageContents" already existsThis tells you the specific conflict blocking the migration.
Verify that your development database matches your migration history. Use a database client (psql, MySQL CLI, or Prisma Studio) to inspect the current schema:
npx prisma studioOr connect directly:
psql $DATABASE_URL -c "\dt" # PostgreSQL: list tables
mysql -u user -p < "SHOW TABLES;" # MySQLIf you see tables or columns that shouldn't exist (created manually or by other tools), you need to remove them or document them in your migrations.
Compare your prisma/schema.prisma with your migration files:
ls -la prisma/migrations/
cat prisma/migrations/*/migration.sqlYour schema.prisma should represent the END state of all your migrations. If it doesn't, edit schema.prisma to match, then generate a new migration:
npx prisma migrate dev --name fix_schema_syncIf your database and schema are out of sync, pull the current database state into your Prisma schema:
npx prisma db pullThis introspects your actual database and updates schema.prisma. Review the changes carefully, then create a migration to document this state:
npx prisma migrate dev --name baseline_from_dbIf you're in active development and the data in your dev database doesn't matter, reset the database to a clean state:
npx prisma migrate resetThis will:
1. Drop the database
2. Recreate it
3. Run all migrations from scratch
4. Re-seed with your seed data
Warning: Only use this in development. Never run this in production.
If the error mentions "permission denied" or similar, ensure your database user has sufficient privileges:
For PostgreSQL:
ALTER USER your_user CREATEDB;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO your_user;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO your_user;
GRANT USAGE ON SCHEMA public TO your_user;For MySQL:
GRANT ALL PRIVILEGES ON *.* TO 'your_user'@'localhost';
FLUSH PRIVILEGES;Alternatively, configure a separate shadow database user with full permissions.
If your hosting provider (e.g., Azure, some managed databases) doesn't allow automatic shadow database creation, specify one manually in schema.prisma:
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
shadowDatabaseUrl = env("SHADOW_DATABASE_URL")
}Then in your .env.local:
DATABASE_URL=postgresql://user:password@host/production_db
SHADOW_DATABASE_URL=postgresql://user:password@host/shadow_dbCreate the shadow database manually and ensure it's empty before running migrations.
If you switched git branches and have conflicting migrations in your history, use prisma migrate resolve to mark migrations as rolled back:
npx prisma migrate resolve --rolled-back migration_name_hereThen run:
npx prisma migrate devThis reconciles your migration history without forcing a full reset.
If the error persists, check for syntax issues in your latest migration file:
cat prisma/migrations/*/migration.sql | tail -20Ensure:
- File is saved as UTF-8 (not UTF-8 with BOM)
- SQL syntax is valid for your database
- Table/column names are properly quoted if needed
If you find an issue, edit the migration SQL file directly (before applying it) or delete the unapplied migration and recreate it.
Advanced notes
Schema Drift Explanation: Prisma compares the end state of your migration history (replayed in the shadow database) against your actual development database. If they don't match, schema drift is detected. This can happen when: (1) you manually alter the database with raw SQL outside of Prisma, (2) you use prisma db push instead of migrate, or (3) migrations are applied partially. To prevent this, always use Prisma Migrate for schema changes and commit migration files to git. Cloud-Hosted Databases: Some managed databases (Heroku, Azure, DigitalOcean) have restrictions on creating/dropping databases. For these, you must either: (a) create a shadow database manually and configure shadowDatabaseUrl, or (b) use prisma db push for prototyping instead of prisma migrate dev. Git Branch Conflicts: When merging branches with different migrations, always re-run your migrations after merging. If branch A and B both added migrations independently, their order might conflict. Use git rebase or manually resolve conflicts in the migrations folder. If you have duplicate migration timestamps, rename them with unique dates.
Frequently asked questions
- What does "P3006: Migration failed to apply cleanly to the shadow database" mean?
- The P3006 error occurs when Prisma Migrate cannot apply your migration to the shadow database during development. The shadow database is a temporary, automatically-created database that Prisma uses to detect schema drift and ensure migrations are safe before applying them to your actual development database. When a migration fails to apply cleanly to this shadow database, it indicates a mismatch between your migration files, your Prisma schema, and your actual database state.
- How do I fix "P3006: Migration failed to apply cleanly to the shadow database"?
- This error occurs when Prisma's shadow database (used for detecting schema drift) cannot apply your migrations cleanly. Common causes include schema drift, permission issues, manual database changes, or conflicting migrations. Resolve it by resetting your database, syncing your schema, or fixing permissions.
- What causes "P3006: Migration failed to apply cleanly to the shadow database"?
- Database schema drift: Your development database was manually modified or has uncommitted manual schema changes that don't match your Prisma schema or migration files Manual migration edits: You edited a migration file after it was already applied to the database Database permission issues: The database user lacks permissions to create/drop tables, indexes, or triggers needed by the migration
- Is the "P3006: Migration failed to apply cleanly to the shadow database" error serious?
- Yes. This is a high-severity error that usually blocks work until it is fixed, but the steps above resolve it.
Official resources & further reading
- 📚Prisma Official Documentation
- 📖Prisma - About the shadow database(Official Docs)
- 📖Prisma - Troubleshooting migrations(Official Docs)
- 📖Prisma - Error reference(Official Docs)
- 🐙
- 📖Prisma - Mental model for Migrate(Official Docs)