AI Workshop: learn to build apps with AI →
Prisma: Using multiple fields for a unique key in Prisma

Join the AI Workshop and learn to build real-world apps with AI. A hands-on, practical program to level up your skills.


I ran into an issue with Prisma that made me lose a bit of time, so I’ll write how I solved it.

The model didn’t have an id field marked as @id so I added a @@unique() to say user and tweet, together, defined the unique constraint.

model Like {
  user      Int
  tweet     Int
  createdAt DateTime @default(now())
  @@unique([user, tweet])
}

This means we can’t have duplicate (user, tweet) entries.

When I tried to delete an entry with

await prisma.like.delete({
  where: {
    user: 1,
    tweet: 1
  }
})

I ran into an error message:

PrismaClientValidationError: 
Invalid `prisma.like.delete()` invocation:

{
  where: {
    user: 12,
    ~~~~
    tweet: 22
    ~~~~~
  }
  ~~~~~~~~~~~
}

Argument where of type LikeWhereUniqueInput needs exactly one argument, but you provided user and tweet. Please choose one. Available args: 
type LikeWhereUniqueInput {
  user_tweet?: LikeUserTweetCompoundUniqueInput
}

What I had to do was change

await prisma.like.delete({
  where: {
    user: 1,
    tweet: 1
  }
})

to

await prisma.like.delete({
  where: {
    user_tweet: {
      user: 1,
      tweet: 1
    }
  }
})

In other words, combining the unique fields concatenating them with an underscore.

In retrospect the error message was sort of explaining this, but I didn’t get it.

Lessons in this unit:

0: Introduction
1: How to use Prisma
2: Prisma relations
3: ▶︎ Using multiple fields for a unique key in Prisma
4: Prisma, how to reverse order
5: Prisma, how to clear the database
6: How to solve the `prisma/client did not initialize yet` error on Vercel