查找具有空关系的记录

Find record with an empty relation

假设我有这个 prisma 模式,其中隐含 m:n-PostTag

关系
model Post {
  id   String @id
  tags Tag[]
}

model Tag {
  id    Int    @id @default(autoincrement())
  posts Post[]
}

如何找到第一个 Post 没有 个关联的 Tag

prisma.post.findFirst({
  where: {
    tags: {
      // are nonexistent (something like count === 0?)
    },
  },
}),

感谢您的帮助:)

在互联网上搜索我发现 link 官方文档中提供了有关如何管理列表的信息 --> here

那里给出了这个例子:

const posts = await prisma.post.findMany({
  where: {
    tags: {
      isEmpty: true,
    },
  },
})

尝试根据您自己的情况进行调整。

你大概可以使用 orderBy 按标签的升序计数并获得第一个?像这样:

prisma.post.findFirst({
  orderBy: { tags: { _count: 'asc' } },
});