Advanced migration scenarios
In some scenarios, you need to edit a migration before applying it. For example, to change the direction of a 1-1 relation (moving the foreign key from one side to another) without data loss, you need to move data as part of the migration - this SQL is not part of the default migration, and must be written by hand.
To edit a migration file before applying it:
Make a schema change that requires custom SQL (for example, to preserve existing data)
Create a draft migration using:
$npx prisma migrate dev --create-onlyModify the generated SQL file.
Apply the modified SQL by running:
$npx prisma migrate dev
Renaming a field
By default, renaming a field in the schema results in a migration that will:
CREATE
a new column (for example,fullname
)DROP
the existing column (for example,name
) and the data in that column
To actually rename a field and avoid data loss when you run the migration in production, you need to modify the generated migration SQL before applying it to the database. Consider the following schema fragment - the biograpy
field is spelled wrong.
model Profile {id Int @id @default(autoincrement())biograpy StringuserId Intuser User @relation(fields: userId, references: id)}
To rename the biograpy
field to biography
:
Rename the field in the schema:
model Profile {id Int @id @default(autoincrement())biograpy Stringbiography StringuserId Intuser User @relation(fields: userId, references: id)}Run the following command to create a draft migration that you can edit before applying to the database:
$npx prisma migrate dev --name rename-migration --create-onlyEdit the draft migration as shown, changing
DROP
/DELETE
to a singleRENAME COLUMN
:BeforeAfter./prisma/migrations/20210308092620_rename_migration/migration.sql1ALTER TABLE "Profile" DROP COLUMN "biograpy",2ADD COLUMN "biography" TEXT NOT NULL;Save and apply the migration:
$npx prisma migrate dev
You can use the same technique to rename a model
- edit the generated SQL to rename the table rather than drop and re-create it.
Changing the direction of a 1-1 relation
To change the direction of a 1-1 relation:
Make the change in the schema:
model User {id Int @id @default(autoincrement())name Stringposts Post[]profile Profile?profileId @relation(fields: profileId, references: id)}model Profile {id Int @id @default(autoincrement())biography Stringuser User}Run the following command to create a draft migration that you can edit before applying to the database:
$npx prisma migrate dev --name rename-migration --create-onlyHide CLI results⚠️ There will be data loss when applying the migration:• The migration will add a unique constraint covering the columns `[profileId]` on the table `User`. If there are existing duplicate values, the migration will fail.Edit the draft migration as shown:
BeforeAfter-- DropForeignKeyALTER TABLE "Profile" DROP CONSTRAINT "Profile_userId_fkey";-- DropIndexDROP INDEX "Profile_userId_unique";-- AlterTableALTER TABLE "Profile" DROP COLUMN "userId";-- AlterTableALTER TABLE "User" ADD COLUMN "profileId" INTEGER NOT NULL;-- CreateIndexCREATE UNIQUE INDEX "User_profileId_unique" ON "User"("profileId");-- AddForeignKeyALTER TABLE "User" ADD FOREIGN KEY ("profileId") REFERENCES "Profile"("id") ON DELETE CASCADE ON UPDATE CASCADE;Save and apply the migration:
$npx prisma migrate dev