Search code examples
mysqlnestjsprisma

One True Value with multiple False Values per Entity based on Relation


I am using Prisma with Nest JS and stumbled upon a Problem:

I have a Picklist Entity, which contains different Picklists which are available for the Frontend. Furthermore, I have one Picklist Values Entity, which contains the Values which are available per Picklist. The Problem I am currently facing is that there can only be one Default Picklist Value per Picklist and multiple other Values which aren't the Default one. Right now, I can only have 2 Picklist Values per Picklist - One Default one and one none Default one.

How could I achieve this?

Picklist Entity:

model Picklist {
  id Int @id @default(autoincrement())

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  picklistName    String @db.VarChar(255)
  picklistApiName String @db.VarChar(255)

  picklistValues PicklistValues[]

  @@map("Picklists")
}

Picklist Values Entity:

model PicklistValues {
  id Int @id @default(autoincrement())

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  value         String  @db.VarChar(255)
  apiName       String  @db.VarChar(255)
  defaultValue  Boolean
  inactiveValue Boolean

  picklistId Int

  picklist      Picklist @relation(fields: [picklistId], references: [id])

  @@unique([picklistId, defaultValue])
}

I know that the Problem comes from the @@unique([picklistId, defaultValue]) but I haven't found a Solution to change it.


Solution

  • Prisma does not have support for creating a Partial index at the moment. There is an open issue in that regard. You can also add your use case in the open issue. You can also create a custom migration and manually add the SQL code for a partial index.