This error occurs when Prisma Migrate fails to execute a migration against your database. The migration process halts, preventing new migrations from being applied until you resolve the underlying issue and reconcile the migration history.
The P3018 error indicates that Prisma Migrate attempted to apply a migration to your database but encountered a failure during execution. When this happens, the migration is recorded as failed in the `_prisma_migrations` table, and Prisma blocks all subsequent migration attempts until you explicitly resolve the situation. This safety mechanism prevents your database from entering an inconsistent state. The failed migration might have partially executed, leaving your database schema in an unknown state between the old and new versions. Prisma requires you to either complete the migration manually and mark it as applied, or roll it back and fix the underlying issue. The error is most commonly encountered during `prisma migrate deploy` in production environments or `prisma migrate dev` in development, and the root cause can range from SQL syntax errors in the migration file to constraint violations caused by existing data.
First, query the _prisma_migrations table to identify which migration failed and read the error details:
SELECT migration_name, logs, finished_at, rolled_back_at
FROM _prisma_migrations
WHERE finished_at IS NULL OR rolled_back_at IS NOT NULL
ORDER BY started_at DESC;The logs column contains the actual database error that caused the failure. Read this carefully to understand what went wrong. Common errors include syntax issues, constraint violations, or missing database objects.
You can also check your Prisma CLI output - it typically displays the error message when the migration fails.
Locate the failed migration in your prisma/migrations/ directory. The migration name from the previous step corresponds to a timestamped folder.
Open the migration.sql file and review it for:
- SQL syntax errors
- Operations that conflict with existing data (e.g., adding NOT NULL without a default)
- References to non-existent tables or columns
- Constraint definitions that existing data violates
If you find an error in the migration file itself, fix it directly in the file. Note that you should only edit migrations that haven't been successfully applied to any environment yet.
Based on the error in the logs, address the underlying issue:
For data constraint violations:
-- Example: Clean up duplicate data before making field unique
UPDATE users SET email = CONCAT(email, '-', id)
WHERE id NOT IN (
SELECT MIN(id) FROM users GROUP BY email
);For missing database objects:
-- Example: Create missing extension
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";For NOT NULL violations:
-- Example: Populate NULL values before adding constraint
UPDATE products SET category = 'uncategorized' WHERE category IS NULL;The goal is to make your database compatible with the migration's requirements.
If the migration partially executed before failing, you need to manually complete the remaining steps. Carefully review the migration file and identify which statements succeeded and which failed.
Execute the remaining SQL statements manually against your database:
# Connect to your database
psql $DATABASE_URL
# Or for MySQL
mysql -h host -u user -p databaseThen run the SQL statements that weren't executed. Be extremely careful to match the migration file exactly - any deviation can cause discrepancies between environments.
For production environments:
If you manually completed the migration, mark it as applied:
npx prisma migrate resolve --applied "migration_name"This updates the _prisma_migrations table to record the migration as successful without re-executing the SQL.
For development environments:
The easier approach is to reset and reapply:
# Reset database (WARNING: deletes all data)
npx prisma migrate reset
# Or mark as rolled back and fix
npx prisma migrate resolve --rolled-back "migration_name"After marking as rolled back, fix the migration file and run:
npx prisma migrate devAfter resolving, confirm all migrations are recorded correctly:
npx prisma migrate statusThis should show all migrations as applied with no failures. You can also query the _prisma_migrations table again:
SELECT migration_name, finished_at, rolled_back_at
FROM _prisma_migrations
ORDER BY started_at DESC
LIMIT 10;All migrations should have a finished_at timestamp and NULL for rolled_back_at. If everything looks correct, you can now apply new migrations.
Shadow Database Considerations
In development, Prisma uses a shadow database to validate migrations. If the shadow database has different state than your main database, migrations can fail in development but succeed in production (or vice versa). Always test migrations against a production-like dataset.
Avoiding Migration Conflicts
Never modify or delete migrations that have been applied to any environment. If you need to change something, create a new migration. Editing applied migrations creates discrepancies between the migration history in your codebase and the _prisma_migrations table, leading to difficult-to-debug issues.
Team Coordination
In team environments, P3018 errors often occur when multiple developers create migrations simultaneously. Always pull the latest migrations from version control before creating new ones, and coordinate schema changes to avoid conflicts.
Baselining Existing Databases
If you're adding Prisma to an existing database with no migration history, use prisma migrate resolve --applied to mark all initial migrations as applied without executing them. This is called baselining and prevents Prisma from trying to recreate tables that already exist.
Transaction Rollback Behavior
Most databases run migrations in a transaction, so a failed migration typically rolls back completely. However, some DDL operations (like CREATE INDEX CONCURRENTLY in PostgreSQL) cannot run in transactions, which can leave partially-applied changes. Always check your database state after failures.
Production Hotfixes
If you must make emergency schema changes directly in production, document them thoroughly and create matching migrations in your codebase afterward. Use prisma migrate resolve --applied to record them without re-executing. See the Prisma documentation on patching and hotfixing for the full workflow.
P1013: The provided database string is invalid
The provided database string is invalid
P1000: Authentication failed against database server
Authentication failed against database server
P1010: User was denied access on the database
How to fix "P1010: User was denied access on the database" in Prisma
P5008: Usage exceeded, upgrade your plan (Accelerate)
How to fix "Usage exceeded, upgrade your plan" in Prisma Accelerate
P3021: Foreign keys cannot be created on this database
How to fix 'P3021: Foreign keys cannot be created on this database' in Prisma