s4eed
s4eed

Reputation: 7891

Add constraint on combination of multiple fields of prisma model

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?

Upvotes: 4

Views: 2115

Answers (1)

Nurul Sundarani
Nurul Sundarani

Reputation: 7558

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.

Upvotes: 4

Related Questions