if there is one-to-maney relation to same table more than one time, we should give these relations names
model User {
id String @id @default(uuid())
name String
email String
isAdmin Boolean
preferences Json
writtenPosts Post[] @relation("WrittenPosts")
favoritePosts Post[] @relation("FavoritePosts")
}
model Post {
id String @id @default(@uuid())
rating Float
createdAt DataTime @default(now())
updatedAt DateTime @updatedAt
author User @relation("WrittenPosts", fields: [authorId], references: [id])
authorId String
favoritedBy User? @relation("FavoritePosts", fields: [favoritedById], references: [id])
favoritedById String?
}many diffrent posts associated with any diffrent tags, normally need intermidiary table, prisam in this case handle this intermiary table for us (create and name it under the hood)
model Post {
id Int @id @default(autoincrement())
title String
tags Tag[]
}
model Tag {
id Int @id @default(autoincrement())
posts Post[]
}prisma.post.findMany({
include: {
tags: true
}
})
// { id: 1, title: 'Post1', tags: [{ id: 2 }] }