Migrations
Manage database schema changes with Prisma Migrate.
Prisma Migrate manages your database schema changes through migration files stored in prisma/migrations. Each migration represents a set of changes to your database structure.
Development Workflow
Modify Your Schema
Update prisma/schema.prisma with your changes:
model Post {
id String @id @default(cuid())
title String
content String
authorId String
createdAt DateTime @default(now())
@@index([authorId])
}Create Migration
Generate and apply the migration:
npx prisma migrate dev --name add_post_modelThis creates a migration file, applies it to your database, and regenerates Prisma Client.
Commit Migration
Add the migration to version control:
git add prisma/migrations
git commit -m "feat: add post model"Migration Commands
Development
# Create and apply migration
npx prisma migrate dev --name migration_name
# Create migration without applying
npx prisma migrate dev --create-only
# Apply pending migrations
npx prisma migrate devProduction
# Apply all pending migrations
npx prisma migrate deploy
# Check migration status
npx prisma migrate statusUse migrate dev in development and migrate deploy in production. Never use migrate dev in production.
Reset Database
# Drop database, recreate, and apply all migrations
npx prisma migrate resetmigrate reset deletes all data. Only use in development or with backups.
Common Scenarios
Adding a Field
model Event {
id String @id @default(cuid())
type String
slug String
text String
timestamp BigInt
views Int @default(0) // New field
}npx prisma migrate dev --name add_views_fieldAdding a Relationship
model Post {
id String @id @default(cuid())
title String
authorId String
author User @relation(fields: [authorId], references: [id])
@@index([authorId])
}
model User {
id String @id @default(cuid())
name String
posts Post[]
}npx prisma migrate dev --name add_post_author_relationRenaming a Field
model Event {
id String @id @default(cuid())
description String // Renamed from 'text'
timestamp BigInt
}npx prisma migrate dev --name rename_text_to_descriptionPrisma detects renames as drop + create. To preserve data, manually edit the migration SQL to use ALTER TABLE ... RENAME COLUMN.
Production Deployment
Vercel
Add to your build command:
{
"scripts": {
"build": "prisma migrate deploy && next build"
}
}Or set in Vercel dashboard:
- Build Command:
prisma migrate deploy && npm run build
Environment Variables
Ensure production has both connection strings:
DATABASE_URL="postgresql://..."
DIRECT_URL="postgresql://..."Best Practices
Descriptive Names
# ✅ Good
npx prisma migrate dev --name add_user_email_verification
# ❌ Bad
npx prisma migrate dev --name updateSmall Migrations
# ✅ Good - one change per migration
npx prisma migrate dev --name add_post_title_field
npx prisma migrate dev --name add_post_content_field
# ❌ Bad - too many changes
npx prisma migrate dev --name add_entire_blog_systemReview Generated SQL
# Create without applying
npx prisma migrate dev --create-only --name your_migration
# Review SQL in prisma/migrations/[timestamp]_your_migration/migration.sql
# Apply if looks good
npx prisma migrate devNext Steps
- Usage & Integration - Use Prisma Client in your code
- Troubleshooting - Resolve migration issues
- Prisma Migrate Docs - Complete reference
How is this guide ?
Last updated on