How to apply unique constraint to multiple fields where 1 field is nullable? #21322
Replies: 1 comment
-
Hi @inkeyygnito-dev 👋 Thank you for raising this question. Prisma does not currently support the However, you can manually add this constraint to your PostgreSQL database and use If this answers your question, it would be great if you could mark this Discussion as answered to indicate that it has been resolved. Otherwise please let us know how else we can help you further or close the Discussion if it was resolved in some other way 🙏 |
Beta Was this translation helpful? Give feedback.
-
Question
I am using prisma with postgres and I am trying to apply a unique constraint on multiple fields where 1 field is nullable. I need the nullable field to be compared as well.
For example with my current model:
I'd like these 2 entries to violate the unique constraint.
But postgres is comparing null != null. In postgres, I can do this by using NULLS NOT DISTINCT:
CONSTRAINT uq_val1_val2_new UNIQUE NULLS NOT DISTINCT (val1, val2);
Is there a way I can tell postgres via prisma to ignore comparision of field if both field is null?
How to reproduce (optional)
Expected behavior (optional)
No response
Information about Prisma Schema, Client Queries and Environment (optional)
// Add your schema.prisma
// Add any relevant Prisma Client queries here
OS:
Database:
Node.js version:
Run
prisma -v
to see your Prisma version and paste itBeta Was this translation helpful? Give feedback.
All reactions