#26251·Prisma

Prisma creates invalid migration when removing an index

Author: dqnykampCreated Feb 5, 2025Updated Sep 29, 2026
Labelsbug/2-confirmedkind/bugtopic: migratetopic: mysqltopic: indexes

Bug description

For the simple schema below, when removing an index and then running a migration, prisma creates a migration that duplicates a previous foreign key constraint name. The duplicate constraint is not directly related to the index removed. The table on which the duplicate foreign key is created also varies depending on the schema. For the simplest schema I could find shown below, the duplicate foreign key was put on the an implicit many-to-many table. But, in other cases (as I was simplifying the schema to find this minimal example), the duplicate foreign key was put on other tables.

It seems that prisma get mixed up between the foreign key of the self-relation "folderStructure", the index being removed (which involves one of the fields of the self-relation), and some random other table (in this case the implicit many-to-many table).

How to reproduce

  1. With the below schema, run prisma migrate dev, creating an initial migration.
  2. Delete the index from the schema on line 19: @@index([parentFolderId, isFolder])
  3. Rerun prisma migrate dev
  4. The result is an error P3018. The database error is always a "Duplicate foreign key constraint name", though the table varies. In this case, the problem is query 3 in the migration.sql file created with the second migration:
ALTER TABLE `_contentTocontentFeatures` ADD CONSTRAINT `_contentTocontentFeatures_B_fkey` FOREIGN KEY (`B`) REFERENCES `contentFeatures`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

The first query in the migration.sql is also appears invalid. It is removing a foreign key constraint from the self-relation:

ALTER TABLE `content` DROP FOREIGN KEY `content_parentFolderId_fkey`;

Expected behavior

The second migration should be created correctly, simply removing the index and not altering any foreign key constraints.

The index removed is not needed for the foreign key constraint, though prisma's migrate code seems to think so. (If I delete the unwanted first and third queries in the second migration.sql file, prisma complains that the removed index is needed.)

Prisma information

prisma
generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["relationJoins"]
}

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}

model content {
  id              Int               @id @default(autoincrement())
  isFolder        Boolean
  parentFolderId  Int?
  parentFolder    content?          @relation("folderStructure", fields: [parentFolderId], references: [id], onDelete: NoAction, onUpdate: NoAction)
  subFolders      content[]         @relation("folderStructure")
  contentFeatures contentFeatures[]

  @@index([parentFolderId, isFolder])
}

model contentFeatures {
  id      Int       @id @default(autoincrement())
  content content[]
}a

Environment & setup

  • OS: Ubuntu 24.04.1
  • Database: MySQL
  • Node.js version: v20.16.0

Prisma Version

6.3.1