PrismaP
Prisma8mo ago
3 replies
lyhourrrr

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
Screenshot_2025-06-06_at_2.07.06_in_the_afternoon.png
Was this page helpful?