P
Prisma2mo 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
moosthuizen2mo 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
moosthuizen2mo ago
Example from docs
No description
moosthuizen
moosthuizen2mo ago
You will need two user entries in the Post model, eg. usersWithThisPost and usersThatFavouritedThisPost
Mattéo
Mattéo2mo ago
Thank you @moosthuizen, I’m going to read this
Want results from more Discord servers?
Add your server
More Posts
prisma is generating some crappy migrations for no reasonHey folks, Can someone guide me why prims is generating some crappy migrations for no reason? I meanPointing two relations to same field?I have a scenario where it make sense to store informations about two 1-n relation in one field. I'Cannot read properties of undefined (reading 'kind')Hey, i get the following error, when performing a db action. ``` [Nest] 43127 - 06/17/2024, 10:06Docker error Can't reach database server at localhost:5432I'm using docker compose and my server api depends on a postgres server in the same container. DurinWhy doesn't the generated client use the .env file specified by dotenv?package.json ```json { "scripts": { "migrate:dev": "dotenv -e .env.development -- npx prismaPrisma seed data erased during testing in single test file runHello all, I've been using prisma for a couple years now so I'm relatively familiar with the ORM andLearning Prisma from 0Hi all, I am following the Prisma docs and I am running into an error message. This is an exisitingDoes Raw Queries Hit Read Replica ?Hey folks There are places where we use raw sql queries which are unavoidable , we have a read repTS Type Issue on Nested SelectsError, console logs, model and query are below. Query returns data as expected. Thanks. ```ts userDstuck with prisma and supabase using nextjs14 when i want to npx prisma db push/ migrate / reset etcEnvironment variables loaded from .env It is almost 24h since i have been stuck in it using nextjs14