1

如何通过过滤获得帖子tagId

我测试了这段代码,但它不起作用:我得到了所有没有过滤的帖子!

prisma.post.findMany({
      include:
      {
        Tags:
        {
          where: { TagId: tagId  },
          include:
          {
            Tag: true
          }
        }
      },
    })

架构.棱镜:

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[]
}

我该如何解决这个问题?

4

1 回答 1

1

您需要在主查询中过滤它,而不是在include. include仅用于获取关系然后在其中进行过滤,它不会影响主查询。

您的最终查询将如下所示:

await prisma.post.findMany({ 
  where: { tags: { some: { tag: { id: 1 } } } } 
})

您可以在此处阅读有关查询关系的更多信息。

于 2021-06-29T05:03:29.810 回答