soft delete with unique constraint
Hello guys ! How to make username unique only for non-deleted users in Prisma?
I'm using Prisma with postgres and I have a User model with a deletedAt field for soft deletes. I want to make sure that username is unique only when deletedAt is null (i.e., for active users). It's okay if deleted users have the same username.
I tried this:
@@unique([username, deletedAt])
But it still allows multiple users with the same username and deletedAt = null.
i will attach my schema in image

3 Replies
Salutations, traveler! I'm the Prisma AI Help Bot. You've reached a fork in the road: one path leads to the wisdom of the human sages (ETA: sometime today), and the other to my instantaneous insights. Which way shall we go?
This is not supported
GitHub
Conditional uniqueness constraints; Partial/Filtered (Unique) Index...
Problem I would like to be able to define a uniqueness constraint that has conditional elements to it so that it can map to and align with a conditional unique index/constraint within the database....