Relation not included in the generated types

Hello, I have the following model, generated from a database-first approach.
model Entry {
project String @db.Uuid
description String
comment String?
status String
projects Projects @relation(fields: [project], references: [uuid], onDelete: NoAction, onUpdate: NoAction, map: "entry_projects_uuid_fk")

@@map("entry")
}

model Projects {
uuid String @id(map: "projects_pk") @default(dbgenerated("gen_random_uuid()")) @db.Uuid
name String
entry Entry[]

@@map("projects")
}
model Entry {
project String @db.Uuid
description String
comment String?
status String
projects Projects @relation(fields: [project], references: [uuid], onDelete: NoAction, onUpdate: NoAction, map: "entry_projects_uuid_fk")

@@map("entry")
}

model Projects {
uuid String @id(map: "projects_pk") @default(dbgenerated("gen_random_uuid()")) @db.Uuid
name String
entry Entry[]

@@map("projects")
}
The query:
const entries: Entry[] = await prisma.entry.findMany({
include: {
projects: true,
},
});
const entries: Entry[] = await prisma.entry.findMany({
include: {
projects: true,
},
});
...returns the following data:
[
{
project: '5d117ed0-1391-4925-bde0-22412825cd54',
description: 'xxxx',
comment: 'xxx',
status: 'xxx',
projects: {
uuid: '5d117ed0-1391-4925-bde0-22412825cd54',
name: 'xxx'
}
}
]
[
{
project: '5d117ed0-1391-4925-bde0-22412825cd54',
description: 'xxxx',
comment: 'xxx',
status: 'xxx',
projects: {
uuid: '5d117ed0-1391-4925-bde0-22412825cd54',
name: 'xxx'
}
}
]
Unfortunately, the generated Prisma type does not include the relation "projects". This means, the following typing does not include the relation. const entries: Entry[] = await prisma.entry.findMany({ ... }) Any idea why that is?
11 Replies
janglad
janglad3mo ago
can you run Prisma generate and try restarting your IDE?
battlesheep123
battlesheep1233mo ago
I did, but it doesn't change anything.
battlesheep123
battlesheep1233mo ago
Could this be a "works as designed"? Sounds very much like this when looking at the docs: 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
janglad
janglad3mo ago
ooh wait sorry I completely misread your question so if you want to manually type this you can do Prisma.EntryGetPayload<{include:{projects:true}}>
battlesheep123
battlesheep1233mo ago
Could you please provide the complete code line? Not sure exactly what you mean. Thanks man! Ahh, I see... do you mean? const entries: Prisma.EntryGetPayload<{ include: { projects: true } }>[] = await prisma.entry.findMany({ ... })
janglad
janglad3mo ago
yup altho FWIW if you're really just doing that I don't see why you'd type it yourself instead of getting the type from the return from findMany
battlesheep123
battlesheep1233mo ago
So you are saying I shouldn't type const entries myself and just keep it as const entries = await prisma.entry.findMany({ ... })?
janglad
janglad3mo ago
yes There are some differing opinions about the extend of this but I personally almost always let TS infer types (so don't type things myself) whenever it's possible
battlesheep123
battlesheep1233mo ago
And you still get code completion if it's typed automatically?
janglad
janglad3mo ago
yes
battlesheep123
battlesheep1233mo ago
Ohh that's great! Thank you very much