How do you filter for records which have no related records using Prisma?

1.6k Views Asked by At

Using the schema from the Prisma docs as an example, I want to query Users for any users which do not have any posts. I can hack it like this, so that it retrieves every user where none of the posts have an ID greater than 0, but it's not very elegant. Is there a better way to do this?

const result = await prisma.user.findMany({
  where: {
    post: {
      none: {
        id: { gt: 0 }
      }
    }
  }
})
1

There are 1 best solutions below

0
On BEST ANSWER

You can do it like this:

prisma.user.findMany({ where: {
  posts: { none: {} }
}})