Using multiple fields for a unique key in Prisma

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 constrain….


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

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 constrain.

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

This means we can’t have more than 1 same entries of (user, tweet) entries.

When I tried to delete an entry with

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

I run 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.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-07-29T05:00:00+00:00) Using multiple fields for a unique key in Prisma. Retrieved from https://www.scien.cx/2021/07/29/using-multiple-fields-for-a-unique-key-in-prisma/

MLA
" » Using multiple fields for a unique key in Prisma." flaviocopes.com | Sciencx - Thursday July 29, 2021, https://www.scien.cx/2021/07/29/using-multiple-fields-for-a-unique-key-in-prisma/
HARVARD
flaviocopes.com | Sciencx Thursday July 29, 2021 » Using multiple fields for a unique key in Prisma., viewed ,<https://www.scien.cx/2021/07/29/using-multiple-fields-for-a-unique-key-in-prisma/>
VANCOUVER
flaviocopes.com | Sciencx - » Using multiple fields for a unique key in Prisma. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/29/using-multiple-fields-for-a-unique-key-in-prisma/
CHICAGO
" » Using multiple fields for a unique key in Prisma." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/07/29/using-multiple-fields-for-a-unique-key-in-prisma/
IEEE
" » Using multiple fields for a unique key in Prisma." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/07/29/using-multiple-fields-for-a-unique-key-in-prisma/. [Accessed: ]
rf:citation
» Using multiple fields for a unique key in Prisma | flaviocopes.com | Sciencx | https://www.scien.cx/2021/07/29/using-multiple-fields-for-a-unique-key-in-prisma/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.