Till
Till

Reputation: 313

How to add type definitions for includes in a Prisma model?

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?

Upvotes: 21

Views: 13584

Answers (1)

Austin Crim
Austin Crim

Reputation: 1267

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

Upvotes: 61

Related Questions