Search code examples
postgresqlmigrationprisma

Prisma auto generates migration even if there are no changes to schema


I have a simple Prisma schema (I'm only using the relevant part):

enum ApprovalStatus {
  APPROVED
  DENIED
  PENDING
}

model Attendee {
  user  User  @relation(fields: [user_id], references: [id])
  user_id BigInt
  event Event @relation(fields: [event_id], references: [id])
  event_id  BigInt
  status  ApprovalStatus @default(APPROVED)
  created_at  DateTime  @default(now())
  updated_at  DateTime?  @updatedAt
  deleted_at  DateTime?

  @@id([user_id, event_id])
  @@unique([user_id, event_id])
  @@map("attendees")
}

After saving the schema I run npx prisma migrate dev, and it creates the migration and successfully migrates. A quick peek in postgres shows that the table is created and a \dT+ shows that the new type and the 3 entries, have been added as well.

Then I noticed that subsequent runs of migration started adding some weird alter table lines for the attendees table, for no reason. I checked the migration and there was no reason for it. Here's the migration of the attendee table, and as you can see status column is quite clearly defined:

-- CreateTable
CREATE TABLE "attendees" (
    "user_id" BIGINT NOT NULL,
    "event_id" BIGINT NOT NULL,
    "status" "ApprovalStatus" NOT NULL DEFAULT 'APPROVED',
    "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at" TIMESTAMP(3),
    "deleted_at" TIMESTAMP(3),

    CONSTRAINT "attendees_pkey" PRIMARY KEY ("user_id","event_id")
);

And now, even if there were no changes to anything in the schema, and all previous migrations were properly applied, running npx prisma migrate dev (with or without --create-only) will always generate a migration with following:

/*
  Warnings:

  - The `status` column on the `attendees` table would be dropped and recreated. This will lead to data loss if there is data in the column.

*/
-- AlterTable
ALTER TABLE "attendees" DROP COLUMN "status",
ADD COLUMN     "status" "ApprovalStatus" NOT NULL DEFAULT 'APPROVED';

It's acting as if the type or name of the column has changed, even though there were no changes to the model or even entire schema for that matter. If I run the generate command more times, it will create this same migration each time with the exact same content. I thought it might have something to do with migration order, but unless it's doing migrations randomly, ApprovalStatus migration comes before attendees does. I really see no reason for it to behave this way, but I'm uncertain how to proceed. Any advice would be welcome.

EDIT: Additional info

"prisma": "^4.6.0"

"express": "^4.17.2"

"typescript": "^4.8.4"

psql (15.0, server 12.13 (Debian 12.13-1.pgdg110+1))


Solution

  • There was a regression introduced in Prisma v4.6.0 where Prisma drops and recreates an enum field as can be seen in this issue. This was fixed in Prisma v4.6.1. Kindly update to the latest version and you should not experience this issue with Prisma migrate.