如何通过过滤tagId
来获取帖子?
我测试了此代码,但它不起作用:我得到所有的帖子没有过滤!
prisma.post.findMany({
include:
{
Tags:
{
where: { TagId: tagId },
include:
{
Tag: true
}
}
},
})
schema.prisma:
model Post {
id Int @id @default(autoincrement())
title String
tags PostTags[]
}
model PostTags {
id Int @id @default(autoincrement())
post Post? @relation(fields: [postId], references: [id])
tag Tag? @relation(fields: [tagId], references: [id])
postId Int?
tagId Int?
}
model Tag {
id Int @id @default(autoincrement())
name String @unique
posts PostTags[]
}
我该如何解决这个问题?
您需要在主查询中过滤它,而不是在include
中。include
仅用于获取关系,然后在其中进行过滤,不会影响主查询。
您的最终查询如下:
await prisma.post.findMany({
where: { tags: { some: { tag: { id: 1 } } } }
})
您可以在此处阅读有关查询关系的更多信息。