Home > other >  How to get Prisma client to generate relationship types
How to get Prisma client to generate relationship types

Time:09-08

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?

CodePudding user response:

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[];
}
  • Related