Had a question about multi column uniqueness. Is ...
# orm-help
j
Had a question about multi column uniqueness. Is there any way to enforce uniqueness, only if both the properties have a value: Say I have a “Team” table with an optional
organizationId String?
field and a required
name String
field. Is there any way to enforce that two teams in the same organization don’t have the same name, BUT also allow many Teams with the same name if they’re not affiliated with an organization (organizationId = null)? So my goal would be : name: “joe”, orgId: null + name:“joe”, orgId:null === OK name: ’joe”, orgId: 1 + name:“joe”, orgId:2 === OK name: ’joe”, orgId: 1 + name:“joe”, orgId:1 === err: DUPLICATE I usually do
@@unique([name, organizationId])
, but i think this would cause the first example to fail when it shouldnt, bc both are null, but i want to allow that.
👀 1
1
n
Is this similar to what you are trying to achieve?https://stackoverflow.com/questions/21637601/how-to-allow-null-value-for-one-column-in-unique-index-on-multiple-columns To me it seems that you want to create a partial multi-column unique index to allow null values.
j
that’s exactly what I want, do you know if that’s achievable via prisma?
looks like theres already an open discussion on it https://github.com/prisma/prisma/issues/3387
n
It’s not natively supported by Prisma yet, you would need to write a custom migration as described in the GitHub discussion.
👍 1