This Prisma Migrate error occurs when a database migration fails during execution and is automatically rolled back. The migration remains in a failed state and must be resolved before new migrations can be applied.
The P3002 error is a Prisma Migrate error that indicates a migration failed to apply completely and was automatically rolled back by the database. When a migration is executed, Prisma attempts to apply all schema changes within a transaction. If any step fails—whether due to syntax errors, constraint violations, or incompatible schema changes—the database automatically reverses all changes made during that migration to maintain data integrity. This error prevents further migrations from running until the failed migration is resolved. The error message includes the underlying database error that caused the rollback, which is essential for diagnosing the root cause. The migration is recorded in the _prisma_migrations table with a failed status, and the logs column contains details about what went wrong. Understanding this error is critical for maintaining a healthy migration workflow, especially in production environments where manual intervention may be required to fix and reapply migrations.
First, examine the error message to identify the underlying database error that caused the rollback:
npx prisma migrate statusYou can also query the _prisma_migrations table directly to see the error logs:
SELECT migration_name, logs, finished_at
FROM _prisma_migrations
WHERE finished_at IS NULL OR logs IS NOT NULL;The logs column contains the detailed database error that will guide your fix. Common errors include constraint violations, type mismatches, or permission issues.
Once you understand why the migration failed, mark it as rolled back so it can be reapplied:
npx prisma migrate resolve --rolled-back "migration_name"Replace "migration_name" with the actual migration folder name (e.g., "20240101120000_add_user_table"). This updates the _prisma_migrations table to register the migration as rolled back, allowing it to be applied again.
Note: This command only works with failed migrations. It cannot be used on successfully applied migrations.
Based on the error logs, edit the migration SQL file to fix the issue. Common fixes include:
For NOT NULL constraints:
-- Add column with a default value first
ALTER TABLE "User" ADD COLUMN "email" TEXT DEFAULT '[email protected]';
-- Then update existing rows
UPDATE "User" SET "email" = 'user_' || id || '@example.com' WHERE "email" IS NULL;
-- Finally, set NOT NULL constraint
ALTER TABLE "User" ALTER COLUMN "email" SET NOT NULL;
ALTER TABLE "User" ALTER COLUMN "email" DROP DEFAULT;For foreign key constraints:
-- Drop dependent foreign keys first
ALTER TABLE "Post" DROP CONSTRAINT "Post_authorId_fkey";
-- Make your changes
ALTER TABLE "User" RENAME COLUMN "id" TO "userId";
-- Recreate foreign keys
ALTER TABLE "Post" ADD CONSTRAINT "Post_authorId_fkey"
FOREIGN KEY ("authorId") REFERENCES "User"("userId");After editing, copy the fixed migration back to your source control.
After fixing the migration file and marking it as rolled back, attempt to reapply it:
For development:
npx prisma migrate devFor production:
npx prisma migrate deployIf the migration still fails, repeat the process: check the logs, fix the issue, mark as rolled back, and try again.
If you're in a development environment and can afford to lose data, the simplest solution is to reset your database:
npx prisma migrate resetThis drops the database, recreates it, applies all migrations from scratch, and runs your seed script. This is the fastest way to recover from failed migrations during local development.
Warning: Never use this in production as it deletes all data.
Production migration strategies: In production environments, you may need to manually complete partially applied migrations. If a migration partially succeeded before rolling back, inspect the database state carefully. You might need to manually revert some changes before marking the migration as rolled back.
Shadow database and development: Prisma uses a shadow database during development to detect schema drift and validate migrations. If your shadow database lacks necessary permissions or resources, migrations may fail. Ensure the shadow database has the same capabilities as your main database.
Handling enum changes: PostgreSQL cannot alter enum types within a transaction. If you're adding or removing enum values, you may need to split the migration into multiple steps or use a workaround that creates a new enum type and migrates data outside the transaction.
Baseline migrations: If you're integrating Prisma Migrate into an existing database, use prisma migrate resolve --applied to mark initial migrations as already applied without running them. This prevents rollbacks due to already-existing schema elements.
Multi-instance deployments: In environments with multiple application instances, ensure only one instance runs migrations at a time. Use deployment orchestration tools to sequence migrations before scaling up instances, or implement migration locks to prevent concurrent execution.
Down migrations: While Prisma Migrate doesn't natively support down migrations, you can use prisma migrate diff to generate a rollback SQL script. Store these alongside your migrations for emergency rollback scenarios in production.
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