Extend client Type loose relation
Hey there, i'm having some trouble when I extend a Prisma client type (in typescript). I have a User, which have a oneToOne relation with a Preferences model, looking like that:
I made this interface:
And I have a function that find a user, aggregate it in a new object with roles array, and the output of this function seems to drop the preferences relation, even if I explicitly include it in my findUnique function. Not an expert in typescript yet, so I may miss something basic, but it should works from what I understand.
Need a lil help on that one please,
Thanks !
Solution:Jump to solution
And adding them manually would have force me to explicitly define all Preferences properties too, since the prisma findUnique function doesn't return a Preferences object, but a custom object based on my query (from what I understand), so the object is not Exactly like the Preferences object would be.
Here is what i done for my issue:
```typescript
export interface UserWithRoles extends Prisma.UserGetPayload<{ include: { preferences: true } }>{...
Operating against partial structures of your model types | Prisma D...
This page documents various scenarios for using the generated types from the Prisma namespace
3 Replies
Hey @DollarNavalex 👋
By default the relation fields are not the part of the generated type, so your
User
type would not have reference to following, followers, preferences etc, relation fields.
If you want to include them, then you need to specify them explicitly.
Here's how you can do that:
https://www.prisma.io/docs/orm/prisma-client/type-safety/operating-against-partial-structures-of-model-types#problem-using-variations-of-the-generated-model-typeOperating against partial structures of your model types | Prisma D...
This page documents various scenarios for using the generated types from the Prisma namespace
Hi, I finally found the Type Helpers on the doc, works much better now, thanks 🙂
Solution
And adding them manually would have force me to explicitly define all Preferences properties too, since the prisma findUnique function doesn't return a Preferences object, but a custom object based on my query (from what I understand), so the object is not Exactly like the Preferences object would be.
Here is what i done for my issue:
And here is a source for type helpers of prisma: https://www.prisma.io/docs/orm/prisma-client/type-safety/operating-against-partial-structures-of-model-types
Operating against partial structures of your model types | Prisma D...
This page documents various scenarios for using the generated types from the Prisma namespace