Handle many-to-many relations in Prisma with MySQL
I am struggling with creating a schema that works in Prisma with MySQL. I've read an article at prisma.io regarding the subject, but couldn't figure out how to solve my own problem.
Here's the schema:
Basically. Every
User
should be able to have several ShoppingLists
. And the ShoppingLists
can contain several Items
.5 Replies
just fix the item model
Solution
you use @relation and add id when you have one to one or one to many
here you have many to many
Ohh okay, cool! Any explanation to why it works like this? Why do you need relation when it's not many-to-many and vice versa?
Saying shoppingList[] and Item[] creates many to many relation in PrismaClient
but for models that are pointing to only one object in a model aka one-to-many and one-to-one
we need to use @relation to create the relation in PrismaClient by saying which field is FK that connects this model to the other model