Prerequisites
- A Prisma project with a Prisma schema
- A Prisma config file (e.g.,
prisma.config.ts) - Database connection configured
Your First Migration
Create the Migration
Run This command will:
prisma migrate dev to create and apply the migration:- Create a new migration directory with timestamp:
prisma/migrations/20201231000000_init/ - Generate a
migration.sqlfile with the SQL statements - Apply the migration to your database
- Generate Prisma Client (if not using
--create-only)
Review the Generated SQL
Prisma Migrate generates SQL based on your schema changes:
prisma/migrations/20201231000000_init/migration.sql
Command Options
Theprisma migrate dev command supports several options:
--name or -n
Name the migration:
--create-only
Create the migration without applying it:
- Review the generated SQL before applying
- Manually edit the migration
- Create an empty migration for custom SQL
--schema
Specify a custom schema path:
--config
Specify a custom config path:
Making Additional Changes
Handling Data Loss Warnings
Some schema changes may result in data loss. Prisma Migrate will warn you:- Type
yto proceed - Type
nto cancel - Use
--accept-data-lossflag to skip the prompt (CI/CD environments)
Handling Unexecutable Steps
Some changes cannot be executed automatically:- Add a default value in your schema
- Make the field optional
- Use
--create-onlyand manually edit the migration
Best Practices
Descriptive Names
Use clear, descriptive migration names that explain the change
Review Generated SQL
Always review the generated SQL before applying in production
Small Migrations
Keep migrations focused on a single logical change
Version Control
Commit migration files to version control
Next Steps
Migration Workflows
Learn development and production workflows
Schema Prototyping
Use db push for rapid iteration