Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add constraint on combination of multiple fields of prisma model

Tags:

prisma

I have this model in Prisma:

model RegisteredPage {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  domain String // instagram, youtube,...
  page   String // s4eed, dive, makeappwithme,...
}

I want to add a constraint on the domain and page so that combination of them is unique. Should I make the combination of them as ID?

like image 938
s4eed Avatar asked Oct 27 '25 13:10

s4eed


1 Answers

You need to use the @@unique attribute.

Unique attributes can be defined on multiple fields using the @@unique attribute, (also called composite or compound unique constraints).

This would define a composite unique index on combination of domain and page fields.

model RegisteredPage {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  domain String // instagram, youtube,...
  page   String // s4eed, dive, makeappwithme,...

  @@unique([domain, page])
}

Here's a Reference for using the @@unique attribute.

like image 61
Nurul Sundarani Avatar answered Oct 29 '25 07:10

Nurul Sundarani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!