prismacockroachdb

How do I resolve Prisma ORM drift after making values non nullable with CockroachDB?


My database is CockroachDB, my Prisma version is 6.11.1.

I added these two models:

model Font {
  id         String        @id @default(uuid())
  name       String        @unique
  created_at DateTime      @default(now())
  deleted_at DateTime?
  variants   FontVariant[] @relation("FontVariants")
}

model FontVariant {
  id           String    @id @default(uuid())
  name         String
  font_id      String
  created_at   DateTime  @default(now())
  deleted_at   DateTime?
  file_link_id String    @unique
  file_link    FileLink  @relation("FontFileLink", fields: [file_link_id], references: [id])
  font         Font      @relation("FontVariants", fields: [font_id], references: [id])
}

Initially, file_link_id and file_link were nullable. My next migration made those fields required. Migration was a success. However, then I added some fields. This is my new schema:

model Font {
  id              String        @id @default(uuid())
  name            String
  variants        FontVariant[] @relation("FontVariants")
  created_at      DateTime      @default(now())
  deleted_at      DateTime?
  owner_id        String
  visibility      Visibility    @default(INTERNAL)
  organization_id String?

  @@unique([name, owner_id])
  @@unique([name, organization_id])
  @@index([owner_id])
}

model FontVariant {
  id           String    @id @default(uuid())
  name         String    @unique
  font_id      String
  font         Font      @relation(fields: [font_id], references: [id], name: "FontVariants")
  created_at   DateTime  @default(now())
  deleted_at   DateTime?
  file_link_id String?   @unique
  file_link    FileLink? @relation(fields: [file_link_id], references: [id], name: "FontFileLink")
}

And then I got this error:

- Drift detected: Your database schema is not in sync with your migration history.

The following is a summary of the differences between the expected database schema given your migrations files, and the actual schema of the database.

It should be understood as the set of changes to get from the expected schema to the actual schema.

[*] Changed the `FontVariant` table
  [*] Altered column `file_link_id` (changed from Nullable to Required)

This is my most recent successful migration, 20250714220438_font_file_link_is_no_longer_optional, the one I believe is causing the problem:

/*
 Warnings:


 - Made the column `file_link_id` on table `FontVariant` required. This step will fail if there are existing NULL values in that column.


*/
-- DropForeignKey
ALTER TABLE "FontVariant" DROP CONSTRAINT "FontVariant_file_link_id_fkey";


-- AlterTable
ALTER TABLE "FontVariant" ALTER COLUMN "file_link_id" SET NOT NULL;


-- AddForeignKey
ALTER TABLE "FontVariant" ADD CONSTRAINT "FontVariant_file_link_id_fkey" FOREIGN KEY ("file_link_id") REFERENCES "FileLink"("id") ON DELETE RESTRICT ON UPDATE CASCADE;

So far, I tried this command: prisma migrate diff, I also tried marking the migrations as applied.

Resetting is not an option. How can I resolve this?


Solution

  • After trying a dozen different things, creating a migration manually seems to do the trick:

    # 1) Create the migration
    mkdir -p prisma/migrations/20250728130000_enforce_file_link_required
    cat > prisma/migrations/20250728130000_enforce_file_link_required/migration.sql <<EOF
    ALTER TABLE "FontVariant" ALTER COLUMN "file_link_id" SET NOT NULL;
    EOF
    
    # 2) Tell Prisma it’s already been applied
    npx prisma migrate resolve \
      --applied 20250728130000_enforce_file_link_required
    
    # 3) Verify / run
    npx prisma migrate status
    npx prisma migrate dev