Skip to main content
Prisma Documentation Docs

Search documentation

Type to search this documentation.

On this pageOverview

Generating down migrations

When generating a migration SQL file, you may wish to also create a "down migration" SQL file that reverses the schema changes in the corresponding "up migration" file. Note that "down migrations" are also sometimes called "migration rollbacks".

This guide explains how to use Prisma Migrate's migrate diff command to create a down migration, and how to apply it to your production database with the db execute command in the case of a failed up migration.

When generating a down migration file, there are some considerations to be aware of:

  • The down migration can be used to revert your database schema after a failed migration using the steps in How to apply your down migration to a failed migration. This requires the use of the migrate resolve command, which can only be used on failed migrations. If your up migration was successful and you want to revert it, you will instead need to revert your schema.prisma file to its state before the up migration, and generate a new migration with the migrate dev command.

  • The down migration will revert your database schema, but other changes to data and application code that are carried out as part of the up migration will not be reverted. For example, if you have a script that changes data during the migration, this data will not be changed back when you run the down migration.

  • You will not be able to use migrate diff to revert manually changed or added SQL in your migration files. If you have any custom additions, such as a view or trigger, you will need to:

    • Create the down migration following the instructions below
    • Create the up migration using migrate dev --create-only, so that it can be edited before it is applied to the database
    • Manually add your custom SQL to the up migration (e.g. adding a view)
    • Manually add the inverted custom SQL to the down migration (e.g. dropping the view)

This section describes how to generate a down migration SQL file along with the corresponding up migration, and then run it to revert your database schema after a failed up migration on production.

As an example, take the following Prisma schema with a User and Post model as a starting point:

title="schema.prisma"
model Post {

  id       Int     @id @default(autoincrement())

  title    String  @db.VarChar(255)

  content  String?

  author   User    @relation(fields: [authorId], references: [id])

  authorId Int

}

model User {

  id    Int     @id @default(autoincrement())

  name  String?

  posts Post[]

}

You will need to create the down migration first, before creating the corresponding up migration.

  • Edit your Prisma schema to make the changes you require for your up migration. In this example, you will add a new Profile model:

    title="schema.prisma"
    model Post {
    
      id       Int     @id @default(autoincrement())
    
      title    String  @db.VarChar(255)
    
      content  String?
    
      author   User    @relation(fields: [authorId], references: [id])
    
      authorId Int
    
    }
    
    model Profile { 
    
      id     Int     @id @default(autoincrement()) 
    
      bio    String?
    
      user   User    @relation(fields: [userId], references: [id]) 
    
      userId Int     @unique
    
    } 
    
    model User {
    
      id      Int      @id @default(autoincrement())
    
      name    String?
    
      posts   Post[]
    
      profile Profile?
    
    }
  • Generate the SQL file for the down migration. To do this, you will use migrate diff to make a comparison:

    • from the newly edited schema
    • to the state of the schema after the last migration

    and output this to a SQL script, down.sql.

    There are two potential options for specifying the 'to' state:

    • Using --to-migrations: this makes a comparison to the state of the migrations given in the migrations directory. This is the preferred option, as it is more robust. To use this option, run:
bunx prisma migrate diff \

  --from-schema prisma/schema.prisma \

  --to-migrations prisma/migrations \

  --script > down.sql
Bash
pnpm prisma migrate diff \
  --from-schema prisma/schema.prisma \
  --to-migrations prisma/migrations \
  --script > down.sql
Bash
yarn prisma migrate diff \
  --from-schema prisma/schema.prisma \
  --to-migrations prisma/migrations \
  --script > down.sql
Bash
npx prisma migrate diff \
  --from-schema prisma/schema.prisma \
  --to-migrations prisma/migrations \
  --script > down.sql
  • Using --to-config-datasource (Prisma v7) or --to-schema-datasource (Prisma 6): this makes a comparison to the state of the database. This does not require a shadow database, but it does rely on the database having an up-to-date schema. To use this option, run:
  • Using --to-config-datasource (Prisma v7) or --to-schema-datasource (Prisma 6): this makes a comparison to the state of the database. This does not require a shadow database, but it does rely on the database having an up-to-date schema. To use this option, run:
title="bun"
bunx prisma migrate diff \

  --from-schema prisma/schema.prisma \

  --to-config-datasource \

  --script > down.sql
pnpm
pnpm prisma migrate diff \
  --from-schema prisma/schema.prisma \
  --to-config-datasource \
  --script > down.sql
yarn
yarn prisma migrate diff \
  --from-schema prisma/schema.prisma \
  --to-config-datasource \
  --script > down.sql
npm
npx prisma migrate diff \
  --from-schema prisma/schema.prisma \
  --to-config-datasource \
  --script > down.sql
  • Generate and apply the up migration with a name of add_profile:
bunx prisma migrate dev --name add_profile
Bash
pnpm prisma migrate dev --name add_profile
Bash
yarn prisma migrate dev --name add_profile
Bash
npx prisma migrate dev --name add_profile

This will create a new <timestamp>_add_profile directory inside the prisma/migrations directory, with your new migration.sql up migration file inside.

  • Copy your down.sql file into the new directory along with the up migration file.

This will create a new <timestamp>_add_profile directory inside the prisma/migrations directory, with your new migration.sql up migration file inside.

  • Copy your down.sql file into the new directory along with the up migration file.

To apply the down migration on your production database after a failed up migration:

  • Use db execute to run your down.sql file on the database server (using the database URL configured in prisma.config.ts):
title="bun"
bunx prisma db execute --file ./down.sql
pnpm
pnpm prisma db execute --file ./down.sql
yarn
yarn prisma db execute --file ./down.sql
npm
npx prisma db execute --file ./down.sql
  • Use migrate resolve to record that you rolled back the up migration named add_profile:
title="bun"
bunx prisma migrate resolve --rolled-back add_profile
pnpm
pnpm prisma migrate resolve --rolled-back add_profile
yarn
yarn prisma migrate resolve --rolled-back add_profile
npm
npx prisma migrate resolve --rolled-back add_profile
Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu