Prisma sync migration gap

1 min read

How to generate a Prisma migration file when the database schema and migration history drift apart.

The Problem

Sometimes your database already contains schema changes that are missing from your Prisma migration files.

In these situations:

  • the actual database structure is ahead
  • but Prisma migration history is behind

The Goal

You want Prisma migrations to catch up with the current database state.

Instead of recreating everything from scratch, you can generate a migration representing the difference between:

  • the current database
  • and your current schema.prisma

Step 1 — Create the Migration Folder

mkdir prisma/migrations/20260515000000_add_file_type

Step 2 — Generate the Migration SQL

npx prisma migrate diff \ --from-config-datasource \ --to-schema prisma/schema.prisma \ --script > prisma/migrations/20260515000000_add_file_type/migration.sql

This generates SQL representing the difference between:

  • the configured datasource database
  • and your Prisma schema

Important Reminder

Always inspect the generated SQL before applying or marking migrations.

Prisma will generate SQL based purely on detected differences.

So if your schema contains accidental changes, those will also appear in the generated migration.


Final Thoughts

prisma migrate diff is one of Prisma's most useful recovery tools.

Especially when working with existing databases or repairing migration drift.

Understanding the difference between:

  • database state
  • Prisma schema
  • migration history

makes Prisma migrations much easier to reason about.