An alternate method (my preferrence) to get the "full" type, or the model with all of its possible relations, is to use the GetPayload version of your model type. The type is a generic that can receive the same object you pass to your query.

import { PrismaClient, Prisma } from "@prisma/client";

const prisma = new PrismaClient();

type UserWithCars = Prisma.UserGetPayload<{
  include: {
    cars: true;
  }
}>

const usersWithCars = await prisma.user.findMany({
  include: {
    cars: true,
  }
});

As your query grows more complex, you may want to abstract it and ensure that it is strongly typed.

import { Prisma, PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

const userInclude = Prisma.validator<Prisma.UserInclude>()({
  cars: true,
});

type UserWithCars = Prisma.UserGetPayload<{
  include: typeof userInclude;
}>;

const usersWithCars = await prisma.user.findMany({
  include: userInclude,
});

Answer from joematune on Stack Overflow
Top answer
1 of 10
103

An alternate method (my preferrence) to get the "full" type, or the model with all of its possible relations, is to use the GetPayload version of your model type. The type is a generic that can receive the same object you pass to your query.

import { PrismaClient, Prisma } from "@prisma/client";

const prisma = new PrismaClient();

type UserWithCars = Prisma.UserGetPayload<{
  include: {
    cars: true;
  }
}>

const usersWithCars = await prisma.user.findMany({
  include: {
    cars: true,
  }
});

As your query grows more complex, you may want to abstract it and ensure that it is strongly typed.

import { Prisma, PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

const userInclude = Prisma.validator<Prisma.UserInclude>()({
  cars: true,
});

type UserWithCars = Prisma.UserGetPayload<{
  include: typeof userInclude;
}>;

const usersWithCars = await prisma.user.findMany({
  include: userInclude,
});

2 of 10
20

You can use the Prisma Validator API along with some typescript features to generate types for your query.

For the findMany example you mentioned

import { Prisma } from '@prisma/client'

// 1. Define a User type that includes the "cars" relation. 
const userWithCars = Prisma.validator<Prisma.UserArgs>()({
    include: { cars: true },
})

// 2: This type will include many users and all their cars
type UserWithCars = Prisma.UserGetPayload<typeof userWithCars>[]

If you simply want to automatically infer the return type of a prisma query wrapped in a function, you can use PromiseReturnType.

For example:

import { Prisma } from '@prisma/client'

async function getUsersWithCars() {
  const users = await prisma.user.findMany({ include: { cars: true } });
  return users;
}

type UsersWithCars = Prisma.PromiseReturnType<typeof getUsersWithCars>

You can read more about this in the Operating against partial structures of your model types concept guide in the Prisma docs.

Discussions

TypeScript: way to reference model type by name ? (`Prisma.*Delegate`)
I'm looking for a way to retrieve the type of a given model when I've already got references to both the PrismaClient and a Prisma.ModelName. My current use case is for middleware that coul... More on github.com
🌐 github.com
3
5
How To Get The Type of a Model?
I'm new to Typescript, and I don't know how to do this. I can't find a type for the generated model... More on github.com
🌐 github.com
1
1
How do I get the model names in a type form? (Prisma) - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Is there a type exported from Prisma that gives me the models as types? More on stackoverflow.com
🌐 stackoverflow.com
how to convert prisma schema to typescript types ?
If you run "prisma generate", you would generate all the ts interfaces based on the prisma schema. You can import those interfaces from "@prisma/client". Example: Prisma Schema: https://ibb.co/P5S53Pj Types generated automatically from the prima Schema: https://ibb.co/QrzbmX8 More on reddit.com
🌐 r/node
5
5
July 6, 2023
🌐
Prisma
prisma.io › home › models › models › models › models
Models | Prisma Documentation
Learn about the concepts for building your data model with Prisma: Models, scalar types, enums, attributes, functions, IDs, default values and more · The data model definition part of the Prisma schema defines your application models (also called Prisma models).
🌐
Codú
codu.co › home › feed › niall maher › how to get specific types from the prisma client with relations
How to Get Specific Types from the Prisma Client with Relations | by Niall Maher | Codú
May 1, 2024 - Here’s an example of how you would do it. Suppose you have a User in your schema that you are querying we would do this: import { PrismaClient, Prisma } from '@prisma/client'; const prisma = new PrismaClient(); // Given this query async function fetchUserWithCars(id: number): Promise<UserWithCars> { return prisma.user.findUnique({ where: { id }, include: { cars: true, }, }); } // How we can generate just the types for the above query type UserWithCars = Prisma.UserGetPayload<{ include: { cars: true; } }>
Find elsewhere
🌐
Medium
medium.com › @jkc5186 › understanding-typescript-types-with-prisma-e0e41a7d98f3
Understanding TypeScript Types with Prisma | by Jeevan KC | Medium
December 17, 2024 - This avoids redefining the model every time. You can quickly reference and extend the original type when needed. For more complex types where relationships exist (e.g., streams, students in a class), Prisma’s GetPayload utility helps extract types including relations.
🌐
Prisma
prisma.io › home › how to use prisma orm's type system › how to use prisma orm's type system › how to use prisma orm's type system › how to use prisma orm's type system
How to use Prisma ORM's type system | Prisma Documentation
When you introspect an existing database, Prisma ORM will take the database type of each table column and represent it in your Prisma schema using the correct Prisma ORM type for the corresponding model field.
🌐
Medium
medium.com › @truongtronghai › orm-prisma-client-models-and-types-e88aee533bf5
ORM Prisma client, models and types | by Truong Trong Hai | Medium
September 19, 2024 - For example: async function ... { return await prisma.post.create({ data, }); } You can directly import the types of your models from the @prisma/client package....
🌐
Prisma
prisma.io › home › type safety overview › type safety overview › type safety overview
Type safety | Prisma Documentation
Payload<Type, Operation>: Retrieves the entire structure of the result, as scalars and relations objects for a given model and operation. For example, you can use this to determine which keys are scalars or objects at a type level.
🌐
Stack Overflow
stackoverflow.com › questions › 79322241 › how-to-get-prisma-model-type-including-relations
reactjs - How to get Prisma model type including relations? - Stack Overflow
But how do I get a typescript type ... without the nested Owner property/relation. Copyimport { Event, Prisma } from "@prisma/client"; type EventDetailsProps = { event: Event; };...
🌐
YouTube
youtube.com › coding in flow
How to Generate Types for Prisma Relation Queries - YouTube
How to use the GetPayload helper to generate TypeScript types for the include filter in your relation queries in Prisma.⭐ Get my full-stack Next.js with Expr...
Published   July 14, 2023
🌐
Prisma Client Python
prisma-client-py.readthedocs.io › en › stable › reference › model-actions
Model Based Access - Prisma Client Python
Instead of using the traditional client based access to methods you can also query directly from the python models themselves: from prisma.models import User user = await User.prisma().create( data={ 'name': 'Robert', }, )
🌐
GitHub
github.com › prisma › prisma › discussions › 3090
How to get all models? · prisma/prisma · Discussion #3090
To reduce the same type of code, I'm trying to implement the mapping of the operation with REST into prisma requests. For example, I have two models: model Locality { id Int @id @default(autoincrement()) name String streets Street[] } model Street { id Int @id @default(autoincrement()) name String localityId Int locality Locality @relation(fields: [localityId], references: [id]) } ... GET /localities/ -> prisma.locality.findMany() POST /localities/ -> prisma.locality.create(...) GET /localities/1/streets -> prisma.street.findMany({where: {...}})
Author   prisma
🌐
Prisma Client Python
prisma-client-py.readthedocs.io › en › stable › getting_started › partial-types
Partial Types - Prisma Client Python - Read the Docs
Just like normal prisma models, partial models can be used anywhere that accepts a pydantic BaseModel. One situation where partial types are particularly useful is in FastAPI endpoints · from typing import Optional from prisma import Prisma from prisma.partials import UserWithoutRelations from fastapi import FastAPI, Depends from .utils import get_db app = FastAPI() @app.get( '/users/{user_id}', response_model=UserWithoutRelations, ) async def get_user(user_id: str, db: Prisma = Depends(get_db)) -> Optional[User]: return await db.user.find_unique(where={'id': user_id})