P
Prisma6mo ago
Mattéo

Multiple relation between models

Hello guys, I'm creating a marketplace but I have a problem. I have 2 models: User and Post. I want the user to have the fields posts (his own posts) and favoritePosts (the posts he liked). I made this but it doesn't work:
model User {
id String @id @default(cuid())
firstName String
lastName String
email String @unique
phone String @unique
password String
verificationCode String?
posts Post[]
favoritePosts Post[]
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
}

model Post {
id String @id @default(cuid())
slug String
title String
description String
price Int
images String[]
reports Report[]
userId String
user User @relation(fields: [userId], references: [id])
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
}
model User {
id String @id @default(cuid())
firstName String
lastName String
email String @unique
phone String @unique
password String
verificationCode String?
posts Post[]
favoritePosts Post[]
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
}

model Post {
id String @id @default(cuid())
slug String
title String
description String
price Int
images String[]
reports Report[]
userId String
user User @relation(fields: [userId], references: [id])
createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt
}
Can you help me please ?
4 Replies
moosthuizen
moosthuizen6mo ago
Relations (Reference) | Prisma Documentation
A relation is a connection between two models in the Prisma schema. This page explains how you can define one-to-one, one-to-many and many-to-many relations in Prisma.
moosthuizen
moosthuizen6mo ago
Example from docs
No description
moosthuizen
moosthuizen6mo ago
You will need two user entries in the Post model, eg. usersWithThisPost and usersThatFavouritedThisPost
Mattéo
MattéoOP6mo ago
Thank you @moosthuizen, I’m going to read this
Want results from more Discord servers?
Add your server