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 }] }
use them if you need add other props to relation as timestamp
or quantity
, or if you need more flexible queries
model Post {
id Int @id @default(autoincrement())
title String
tags PostTag[]
}
model Tag {
id Int @id @default(autoincrement())
posts PostTag[]
}
model PostTag {
postId Int
post Post @relation(fields: [postId], references: [id])
tagId Int
post Tag @relation(fields: [tagId], references: [id])
@@id([postId, tagId])
}
prisma.post.findMany({
include: {
tags: true
}
})
// { id: 1, title: 'Post1', tags: [{ id: 2 }] }