Apr 19, 2025

Clean & Safe Prisma Migrations Guide

Go back
Clean & Safe Prisma Migrations Guide

The Ultimate Guide to Safe and Clean Prisma Migrations

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.


1. Initial Setup: Getting Started with Prisma

Before you start creating migrations, you need to set up Prisma in your project.

Step 1: Initialize Prisma

npx prisma init

This creates the following files:

  • prisma/schema.prisma

  • .env file with DATABASE_URL

Step 2: Configure Database URL

Update your .env:

DATABASE_URL="postgresql://username:password@localhost:5432/mydb"

Step 3: Define Your Initial Schema

model User {
  id        String   @id @default(uuid())
  email     String   @unique
  createdAt DateTime @default(now())
}

Step 4: Create the First Migration

npx prisma migrate dev --name init

This will:

  • Create a migrations/ folder

  • Apply the schema to your local database

  • Generate the Prisma Client


2. Making Schema Changes

Adding a New Field Safely

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"))

Example:

role String @default("user")
// OR
bio String?

Then create the migration:

npx prisma migrate dev --name add-role-and-bio

Renaming a Field Without Losing Data

By default, Prisma doesn’t detect renaming; it treats it as a drop + create.

Step-by-step:

  1. Rename the field in schema.prisma:

    - fullName String
    + name     String
  2. Create the migration without applying:

    npx prisma migrate dev --name rename-fullname-to-name --create-only
  3. Open the generated SQL in prisma/migrations/.../migration.sql and manually change:

    ALTER TABLE "User" RENAME COLUMN "fullName" TO "name";
  4. Apply the migration:

    npx prisma migrate String

This way, you preserve the data while reflecting schema changes.


3. Prisma Migration Naming Convention

A clean and consistent naming strategy helps maintain clarity in your codebase and during collaboration.

📘 Recommended Format:

<action>_<target>[_extra_info]

✅ Examples:

Migration NameDescriptionadd_bio_to_userAdds bio field to User tablerename_createdAt_to_joinedAtRenames a fielddrop_old_comments_tableRemoves comments table

❌ Avoid:

  • temp-migration

  • migration123

  • fix-thing

Use descriptive and structured names to make your git history and collaboration smoother.


4. Production Deployment: The Right Way

When deploying to production, never use db push. Instead, use:

This ensures your production database follows the exact steps recorded in your migration files.


5. Bonus Tips & Best Practices

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


Wrapping Up

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 😉

Related.

Related.

Related.

Related.