Hi all, is it possible to create a uniqueness cons...
# orm-help
h
Hi all, is it possible to create a uniqueness constraint based on the values of a field?
Copy code
model Conversation {
  id           String    @id @default(uuid())
  channelId    String
  active       Boolean   @default(true)
  messages     Message[]
  participants User[]
  summary      String?
  createdAt    DateTime  @default(now())
  updatedAt    DateTime  @updatedAt

  @@unique([channelId, active], name: "active_conversation")
}
I was hoping to create a constraint that there is only ever 1 active (active = true) conversation in a channel, but I realized is that with the constraint as shown above, it actually prevents non-active conversations from being stored if there is already a non-active conversation in the same channel
1
h
I would prefer a runtime check here. It will be tricky to maintain constraint on database level as it is also dependent on
channelId
. Prisma also doesn’t support SQL Check constraints yet: https://github.com/prisma/prisma/issues/3388 so it is also not possible right now to store constraint information in the schema.