Home > Back-end >  How to add type definitions for includes in a Prisma model?
How to add type definitions for includes in a Prisma model?

Time:03-12

The example in the documentation looks like this:

const getUser = await prisma.user.findUnique({
  where: {
    id: 1,
  },
  include: {
    posts: {
      select: {
        title: true,
      },
    },
  },
})

But when I want to read the property getUser.posts I get the following error:

TS2339: Property 'posts' does not exist on type 'User'.

Where can I find the correct type definitions the for the includes option?

CodePudding user response:

The generated types do not include relations because queries don't return relations by default. To include the related models in your type, use the provided Prisma utility types like so:

import { Prisma } from '@prisma/client'
type UserWithPosts = Prisma.UserGetPayload<{
  include: { posts: true }
}>

  • Related