Generated prisma types are missing relationships in their exported type. #10928
-
Bug descriptionSo I am really not sure if this is a bug, my mistake or some really stupid feature. I have a model called Faction, this faction creates a one to many relationship with users (noted as Oddly, if I export For example I am working on a helper where I am running the same function (gets the faction through a different relationship) but I can't actually type it, because if I set the return as How to reproduce
Expected behaviorI would expect, that there is some functionality that allows me to use the types generated by prisma that will contain the owner as well as the faction and should have some kind of logic to allow this? Prisma information
Environment & setup
Prisma Version
|
Beta Was this translation helpful? Give feedback.
Replies: 19 comments 38 replies
-
Hey @dannysmc95 👋 ! Since Prisma queries do not include relations by default (you have to use the import { Prisma } from '@prisma/client'
type FactionWithOwner = Prisma.FactionGetPayload<{
include: { owner: true }
}> Here we use the Let me know if you have any questions about this! |
Beta Was this translation helpful? Give feedback.
-
If you do a fallback call of your prisma client functions, you can just type it correctly like below and you should have the same behaviour as if you directly call the prisma client: async findMany<T>(args: Prisma.SelectSubset<T, Prisma.FactionFindManyArgs>) {
return this.prismaClient.faction.findMany<T>(args);
} |
Beta Was this translation helpful? Give feedback.
-
Is there really no better option to get proper typing here? This seems like a very-very common usecase and it adds tremendous complexity. |
Beta Was this translation helpful? Give feedback.
-
We should atleast add
|
Beta Was this translation helpful? Give feedback.
-
+1 I think this should be a feature request or at least add to the documentation how to handle this use case for Typescript. Industry is moving towards Typescript, having this out of the box would be nice. |
Beta Was this translation helpful? Give feedback.
-
A similar discussion included a link to this docs page about advanced type usage, and I thought it might be helpful here as well. |
Beta Was this translation helpful? Give feedback.
-
This is one of the pain that I am having also. When there are different variations, the codebase gets bloated with so many custom types. It would be great if generator can provide a type with optional relationships, so the developer can decide to use that or create custom types if want to be specific. |
Beta Was this translation helpful? Give feedback.
-
You can use Zod Prisma Types plugin for this. It generates zod schemas which you can ignore but it also generates types with relations . |
Beta Was this translation helpful? Give feedback.
-
Frustrating, I tried every solution posted here and it just doesn't work. It is just one more reason to really just drop Prisma and go back to using a better ORM :) |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
I also need this type of functionality. This seems very basic and needed. Isn't there a way to add a flag to |
Beta Was this translation helpful? Give feedback.
-
This is what I do: import { Prisma } from "@prisma/client";
import { Types } from "@prisma/client/runtime/library";
type PayloadToModel<T> = Types.Result.DefaultSelection<T>;
export type WithRelation<
T extends keyof Prisma.TypeMap["model"],
R extends keyof Prisma.TypeMap["model"][T]["payload"]["objects"]
> =
PayloadToModel<Prisma.TypeMap["model"][T]["payload"]> &
{
[K in R]: PayloadToModel<Prisma.TypeMap["model"][T]["payload"]["objects"][K]>
}; And then reference it as: import type { WithRelation } from "@helpers";
function foo(model: WithRelation<"User", "comments" | "posts">) { ... } I wrote out a bunch of types to make it so that it takes in |
Beta Was this translation helpful? Give feedback.
-
Came here through Google looking after facing the same issue. The first answer worked, but I imagine there can be a better-scaling solution, as I imagine this can get tedious quickly in larger projects. |
Beta Was this translation helpful? Give feedback.
-
I’m using the marked answer for now but I agree, this is a super common use case and a better solution would be very useful. +1 |
Beta Was this translation helpful? Give feedback.
-
Since this is the first google search result, I'll add my comment here. from the prisma docs const videoWithVotes = Prisma.validator<Prisma.VideoDefaultArgs>()({
include: { votes: true }
});
type VideosWithVotes = Prisma.VideoGetPayload<typeof videoWithVotes>; |
Beta Was this translation helpful? Give feedback.
-
i would expect prisma to generate or maybe even have it on |
Beta Was this translation helpful? Give feedback.
-
Found this library which might be of some help for now: https://github.com/Kalissaac/prisma-typegen |
Beta Was this translation helpful? Give feedback.
-
why aren't they included in by default? my work around, invocles a deepy nested type decleration. surely there must be a better way? |
Beta Was this translation helpful? Give feedback.
-
You can always just make your own type this way too by pulling in default types:
|
Beta Was this translation helpful? Give feedback.
Hey @dannysmc95 👋 !
Since Prisma queries do not include relations by default (you have to use the
include
option), the generated types do not include them either. You can create the type you are looking for using one of our built-in utility types, though.Here we use the
FactionGetPayload
type and pass in some query options as the generic parameter to get the desired type.Let me know if you have any questions about this!