question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Conditional uniqueness constraints

See original GitHub issue

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.

Suggested solution

Modify the @@unique modifier to include a where: { } argument that could define the condition that allows this to be unique.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:57
  • Comments:18 (2 by maintainers)

github_iconTop GitHub Comments

19reactions
ArthurThorntoncommented, May 6, 2021

One example of how this might be used would be a model of phone numbers for a user, where we have a field to determine which one is the primary number:

model PhoneNumber {
  (standard id/created/updated fields)

  userId    String  @map("user_id")
  user      User    @relation(fields: [userId], references: [id])
  number    String
  isPrimary Boolean @map("is_primary")

  @@unique([userId, isPrimary], name: "uniquePrimaryForUser", where: { isPrimary: true })
  @@unique([userId, number], name: "uniqueNumberForUser")
}

In postgres, they support unique indexes with conditions. For this example, you’d create the index with:

CREATE UNIQUE INDEX phone_number_unique_primary_for_user ON "PhoneNumber"(user_id, is_primary) WHERE (is_primary = true);

This is possible within Rails’ ActiveRecord validations, which I’ve used in the past, and something I would derive great value from also getting within Prisma.

18reactions
vthang95commented, Oct 14, 2021

Any update on this issue? Very surprised that this is common but unimplemented yet.

This proposal is okay:

@@unique([slug, isPublished], name: "uniquePublishedPostSlug", where: { isPublished: true })

or

@@unique([slug, isPublished], name: "uniquePublishedPostSlug", where: "isPublished = true")
Read more comments on GitHub >

github_iconTop Results From Across the Web

conditional unique constraint - sql - Stack Overflow
I have a situation where i need to enforce a unique constraint on a set of columns, but only for one value of...
Read more >
Conditional Unique Constraint - MSDN - Microsoft
I want to introduce a unique constraint on email but only when a particular setting in a different table is set to true....
Read more >
postgresql - Adding condition to Unique Constraint
This is effectively pretty much the same as a unique constraint, because such constraints are implemented with unique indexes anyway.
Read more >
Name the Relational Violation Part 1: Conditional Uniqueness ...
"I'm seeing more [data] professionals implementing the following type of conditional unique constraints, typically related to the use of 'soft deletes'):.
Read more >
Difference between Unique Indexes and Unique Constraints ...
Overview of UNIQUE constraints in SQL Server ... We can ensure unique value in a column of SQL Server. It can be either...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found