Apr 19, 2025
Managing database schema changes is a critical part of modern application development. With Prisma ORM, developers have a powerful tool to handle migrations easily. However, careless migrations can lead to data loss or broken production environments.
In this guide, we'll walk you through the best practices for handling Prisma migrations safely, how to name your migrations clearly, and how to avoid common pitfalls.
Before you start creating migrations, you need to set up Prisma in your project.
npx prisma init
This creates the following files:
prisma/schema.prisma
.env
file with DATABASE_URL
Update your .env
:
DATABASE_URL="postgresql://username:password@localhost:5432/mydb"
model User {
id String @id @default(uuid())
email String @unique
createdAt DateTime @default(now())
}
npx prisma migrate dev --name init
This will:
Create a migrations/
folder
Apply the schema to your local database
Generate the Prisma Client
When adding a new field to a table that already contains data, you must ensure one of the following:
The field is nullable (String?
)
The field has a default value (@default("value")
)
role String @default("user")
// OR
bio String?
Then create the migration:
npx prisma migrate dev --name add-role-and-bio
By default, Prisma doesn’t detect renaming; it treats it as a drop + create.
Rename the field in schema.prisma
:
- fullName String
+ name String
Create the migration without applying:
npx prisma migrate dev --name rename-fullname-to-name --create-only
Open the generated SQL in prisma/migrations/.../migration.sql
and manually change:
ALTER TABLE "User" RENAME COLUMN "fullName" TO "name";
Apply the migration:
npx prisma migrate String
This way, you preserve the data while reflecting schema changes.
A clean and consistent naming strategy helps maintain clarity in your codebase and during collaboration.
<action>_<target>[_extra_info]
Migration NameDescriptionadd_bio_to_user
Adds bio
field to User
tablerename_createdAt_to_joinedAt
Renames a fielddrop_old_comments_table
Removes comments
table
temp-migration
migration123
fix-thing
Use descriptive and structured names to make your git history and collaboration smoother.
When deploying to production, never use db push
. Instead, use:
This ensures your production database follows the exact steps recorded in your migration files.
TaskBest PracticeAdd new fieldUse @default
or make it nullableRename a fieldUse --create-only
and ALTER TABLE RENAME COLUMN
manuallyLocal resetUse npx prisma migrate reset
(development only!)Team commitAlways commit schema.prisma
and migrations/
folder
Prisma offers powerful tooling to handle migrations smoothly. By following this guide:
You’ll avoid unintentional data loss
Maintain a clean and collaborative schema history
Have confidence in deploying changes to production
Keep your migrations clean, clear, and cautious — and your database will thank you!
Need help automating this in CI/CD or on Vercel? We’ve got a tutorial for that coming soon 😉