Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial unique indexes are being recreated on every migration with prisma migrate #13417

Open
JoniBR opened this issue May 19, 2022 · 6 comments
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/schema Issue for team Schema. topic: endless migrations topic: indexes topic: partial index topic: postgresql

Comments

@JoniBR
Copy link

JoniBR commented May 19, 2022

Bug description

We are using partial unique indexes, since we are implementing soft delete-adding deletedAt column to our models.
Since we want the columns to still behave as unique, we mark them with @unique,
and modify the migration file to add where ("deletedAt" is null) to the index creation.
For example:

CREATE UNIQUE INDEX "Employee_profileId_key" ON "Employee"("profileId") WHERE ("deletedAt" IS NULL);

Whenever we want to create a new migration file this warning shows:

• A unique constraint covering the columns `[profileId]` on the table `Employee` will be added. If there are existing duplicate values, this will fail.

although the index is already created, and the created index statement is added to the migration file.

How to reproduce

  1. Mark a column as unique on the schema file.
  2. Modify the migration file and add a condition to the index.
  3. Run the migration.
  4. Create a new migration.

Expected behavior

Should not try to create unique indexes again.

Prisma information

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

generator client {
  provider        = "prisma-client-js"
}

model Employee {
  id            String       @id @default(cuid())
  profileId     Int          @unique()
  deletedAt DateTime?

}

Environment & setup

  • OS: MacOS Montrerey
  • Database: PostgreSQL
  • Node.js version: v16.14.0

Prisma Version

3.14.0
@JoniBR JoniBR added the kind/bug A reported bug. label May 19, 2022
@janpio janpio added topic: indexes team/schema Issue for team Schema. topic: endless migrations bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. topic: postgresql labels May 23, 2022
@jackbravo
Copy link

jackbravo commented Jun 3, 2022

related to #3076 and #6974

@kicozaninetti
Copy link

kicozaninetti commented Sep 22, 2023

Still same problem in version 5.3

@@index([index_field, created_at(sort: Desc)], map: "index_field_created_at_index")

When "prisma migrate dev" to deletes the index and re-create it again, generating a new migration folder.

@jove4015
Copy link

jove4015 commented Jan 4, 2024

@janpio I appreciate that the best fix for this issue would be to support conditional uniqueness. However, it seems like it's going to take a long time for Prisma to get to this, if it ever does. Is there any way in the meantime to prioritize simply having Prisma ignore the conditions so that it doesn't try to keep recreating the index? That's all the OP is really asking for.

@janpio
Copy link
Member

janpio commented Feb 16, 2024

Simple answer: No. The correct way to deal with this is for Prisma to support partial indexes.
We can't spend implementation time on a configuration option to ignore specific things, that would just be obsolete when the actual feature is implemented.

@trevorr
Copy link

trevorr commented Feb 29, 2024

@janpio We agree that the correct way is supporting partial indexes, but since that feature hasn't progressed since it was requested nearly 4 years ago, maybe it's time to consider enabling a workaround?

@benjidotsh
Copy link

benjidotsh commented Mar 15, 2024

This is a really frustrating issue considering this exact issue is one of the reasons we switched from TypeORM to Prisma...

I get that Prisma can't support every niche feature a RDBMS has to offer like partial indexes, but if Prisma truly wants to "simplify working and interacting with databases", at least give us the choice to take matter into our own hands without getting in the way.

Ideally, we comment the wrong part of the migration out and it doesn't appear anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/schema Issue for team Schema. topic: endless migrations topic: indexes topic: partial index topic: postgresql
Projects
None yet
Development

No branches or pull requests

7 participants