How to rename a table or column using Prisma migrations

August 15, 2022

Max Musing
Founder

Prisma’s migration system is great because it’s able to automatically generate SQL migrations based on changes to your schema.prisma file. However, it’s not always possible to understand your intentions when you make a change. In these cases, you have to manually edit the generated SQL migration. Most commonly, this happens when you want to rename a table or column in your database.

In these cases, you need to manually edit the generated SQL migration to fit your intentions.

How to rename a table with Prisma migrations

  • Edit the name of your model in your schema.prisma file.
  • Run prisma migrate dev --create-only. This will generate the SQL migration file, but will not immediately run it.
  • You’ll be prompted with a warning that you’re about to drop a table. This is ok because we’ll manually fix the migration. Type y to proceed.
  • You’ll be prompted to enter a name for your migration.
  • Open the newly generated migration.sql file in the prisma/migrations directory. Notice that it drops the original table and creates a whole new table. We don’t want this, since it will delete all existing data in that table. The generated SQL should look something like this:

-- DropTable
DROP TABLE "OldName";

-- CreateTable
CREATE TABLE "NewName" ();
  • Edit the SQL query to simply rename the table, like this:

ALTER TABLE "OldName" RENAME TO "NewName";
  • Run prisma migrate dev to run your modified migration.

How to rename a column with Prisma migrations

  • Edit the name of your column in your schema.prisma file.
  • Run prisma migrate dev --create-only. This will generate the SQL migration file, but will not immediately run it.
  • You’ll be prompted with a warning that you’re about to drop a column. This is ok because we’ll manually fix the migration. Type y to proceed.
  • You’ll be prompted to enter a name for your migration.
  • Open the newly generated migration.sql file in the prisma/migrations directory. Notice that it drops the original column and creates a whole new column. We don’t want this, since it will delete all existing data in that column. The generated SQL should look something like this:

-- AlterTable
ALTER TABLE "MyTable" DROP COLUMN "oldName",
ADD COLUMN "newName" INTEGER;
  • Edit the SQL query to simply rename the table, like this:

ALTER TABLE "MyTable" RENAME COLUMN "oldName" TO "newName";
  • Run prisma migrate dev to run your modified migration.

How to migrate your database with zero downtime

With simple migrations like this, there may be a short period of downtime (usually seconds) between migrating your database and updating your server. As a more advanced technique, you can use the “expand and contract” pattern to migrate your database with zero downtime.

Check out the Prisma docs for more details on how this method works: Link to Prisma docs

Verifying that your migration ran successfully

Once you’ve made these changes, you should check the schema of your database to make sure that your migration ran correctly. Specifically, you should check that:

  1. Your changes were successfully applied to your database
  2. All existing data remained intact following the migration

You can do this with a simple SQL client like Arctype or TablePlus. You can also do this with a tool like Basedash, which also lets you build internal tools on top of your database in seconds.

What is Basedash?

Ship your product faster.
Worry about internal tools less.

No credit card required.

More posts like this