Fix Prisma Migrate Error – Migration Fails to Apply
In this tutorial, you'll learn about Fix Prisma Migrate Error. We cover key concepts, practical examples, and best practices.
You run npx prisma migrate dev and get "Error: Environment variable not found: DATABASE_URL" — or the migration creates a database and then fails with "Error: Database error: relation 'users' already exists".
Wrong ❌
npx prisma migrate dev --name add_users
Error:
Error: Environment variable not found: DATABASE_URL
Or:
Error: Database error:
ERROR: relation "users" already exists
The migration tries to create a table that already exists in the database. Your local database has stale tables from a previous run.
Right ✅
# .env
DATABASE_URL="postgresql://postgres:password@localhost:5432/mydb?schema=public"
// schema.prisma
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
createdAt DateTime @default(now())
}
Reset the database and re‑migrate:
# Reset (drops all tables, re‑runs all migrations)
npx prisma migrate reset
Or create a baseline migration for an existing database:
# If the database already has tables created manually:
npx prisma migrate dev --name init --create-only
# Then edit the SQL to match existing schema
npx prisma migrate dev
Fix "relation already exists":
# Check if the database has existing tables
psql -U postgres -d mydb -c "\dt"
# If tables exist outside Prisma's management:
# Option 1: Delete the database and start fresh
npx prisma migrate reset
# Option 2: Baseline — mark existing state as migrated
npx prisma migrate resolve --applied "20240101000000_init"
Diagnose migration errors:
npx prisma migrate status
# Shows pending and applied migrations
npx prisma migrate diff --from-empty --to-schema-datamodel schema.prisma
# Shows the SQL that Prisma would generate
Shadow database issues:
Prisma creates a shadow database for diffing. If the shadow DB connection fails:
# Use the same DATABASE_URL with a different database name
# Or set the shadow database URL explicitly:
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
shadowDatabaseUrl = env("SHADOW_DATABASE_URL") // optional
}
Root Cause
Common migration errors: (1) DATABASE_URL env var is not set or loaded; (2) the target database already has tables that conflict with the migration; (3) the shadow database is missing or inaccessible.
Prevention
- Always source
.envbefore running Prisma commands:dotenv -e .env -- npx prisma migrate dev. - Run
prisma migrate resetbefore starting a new feature that changes the schema. - Use
prisma migrate dev --create-onlyfor complex migrations that need manual SQL tweaks. - Keep migrations in version control — they are the source of truth for schema history.
Common Mistakes with migrate error
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
These mistakes appear frequently in real-world PRISMA code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Prisma migrations are covered in the DodaTech Prisma Fundamentals course.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro