3

I am trying to create a one-to-many relationship between a user and posts. I can create the database schema and can write / query all ok but it appears the prisma client that is being generated doesn't have the relationships on the type.

For example, my schema looks like this:

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

model Post {
  id       Int  @id @default(autoincrement())
  author   User @relation(fields: [authorId], references: [id])
  authorId Int
}

I then run npx prisma generate which creates a client with the following types

/**
 * Model User
 * 
 */
export type User = {
  id: number
}

/**
 * Model Post
 * 
 */
export type Post = {
  id: number
  authorId: number
}

I would expect the user type to look like this:

export type User = {
  id: number;
  posts: Post[];
}

Have I defined my Prisma schema correctly? What do I need to do to get the type to include the relation?

1
  • I've noticed that the ctx.prisma.menu.findFirst method has a return type of (User & { posts: Post[]; }) Is the only way to combine the types manually?
    – Stretch0
    Commented Sep 6, 2022 at 18:36

1 Answer 1

7

By default, a Prisma type will not include relational fields in the base "type". If you want to get the type of your User which includes related Posts, the Prisma client will include that based on the structure of your query.

https://www.prisma.io/docs/concepts/components/prisma-client/select-fields

You can use the include attribute on any Prisma CRUD operation to have the Prisma client return specific relational fields, AND those related fields are included in the emitted type. Prisma will take care of this typing for you!

const usersWithPosts = await prisma.user.findMany({
  include: {
    posts: true
  }
});

// usersWithPosts has a type like this:
// Array<User & { posts: Array<Post> }>

========

EDIT: If you want to compute the type in advance without wanting to write a query, you can use Prisma.UserGetPayload (or some other form of GetPayload, as the prefix will change based on the model.)

type UserWithPosts = Prisma.UserGetPayload<{
  include: {
    posts: true;
  };
}>;

This emits the following type:

type UserWithPosts = User & {
    posts: Post[];
}
2
  • Thanks Shea. I understand the prisma client handles the return types and how to use includes. I was wondering if Prisma generated these types that include the related field for us because it is a bit cumbersome having to manually extend the type of User each time
    – Stretch0
    Commented Sep 7, 2022 at 16:32
  • @Stretch0 Looks like you can leverage Prisma.UserGetPayload (User will change based on your model.) I've updated my original answer! Commented Sep 7, 2022 at 16:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.