How do I query prisma relations while returning all fields on the parent object?

Viewed 195

I am trying to return a post with all comments that have been approved. So I want all fields from the post object and all comments with approved set to true. It seems like this query is only giving me comments. How do I modify it to get what I want?

  const post = await prisma.post.findUnique({
    where: { slug },
    select: { Comment: { where: { approved: true } } },
  });

Thanks for any help.

1 Answers

You need to use include instead of select.

  const post = await prisma.post.findUnique({
    where: { slug },
    include: {
      Comment: {
        where:{
          approved: true
        }
      },
    },
  });

include defines which relations are included in the result that the Prisma Client returns: Reference

Related