-
Bug descriptionIt is very easy to reproduce this, I don't know if it work as intended or not. So I have this simple prisma schema, basically just a 3 relation table, A have B children, B have C children. All have 1 to many relationship. model A {
id Int @id @default(autoincrement())
col1 String @db.VarChar
col2 String @db.VarChar
listB B[]
}
model B {
id Int @id @default(autoincrement())
col1 String @db.VarChar
col2 String @db.VarChar
baId Int?
aData A? @relation(fields: [baId], references: [id], onDelete: Cascade, onUpdate: Cascade)
listC C[]
}
model C {
id Int @id @default(autoincrement())
col1 String @db.VarChar
col2 String @db.VarChar
cbId Int?
bData B? @relation(fields: [cbId], references: [id], onDelete: Cascade, onUpdate: Cascade)
} then in my typescript, I keep it simple: import { Prisma, PrismaClient } from '@prisma/client'
export const db = new PrismaClient()
const selectA: Prisma.ASelect = {
id: true,
col1: true,
col2: true
}
const selectB: Prisma.BSelect = {
id: true,
col1: true,
col2: true,
aData: {
select: selectA
}
}
const selectC: Prisma.CSelect = {
id: true,
col1: true,
col2: true,
bData: {
select: selectB
}
}
const query1 = await db.c.findMany({
select: selectC
})
const result1 = query1[0].bData.aData //error
const result2 = query1[0].bData?.baId
const query2 = await db.c.findMany({
select: {
id: true,
col1: true,
col2: true,
bData: {
select: {
id: true,
col1: true,
col2: true,
aData: {
select: {
id: true,
col1: true,
col2: true
}
}
}
}
}
})
const result3 = query2[0].bData?.aData
const result4 = query2[0].bData?.baId //error Result 1 throwing typescript error because aData is not exist in that type, while result2 works even though I'm not specifying it in the selectB. While the result 3 and 4 is working normally. How to reproduceExpected behaviorSelect working normally, typescript not throwing any error like in the query2 Prisma informationgenerator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model A {
id Int @id @default(autoincrement())
col1 String @db.VarChar
col2 String @db.VarChar
listB B[]
}
model B {
id Int @id @default(autoincrement())
col1 String @db.VarChar
col2 String @db.VarChar
baId Int?
aData A? @relation(fields: [baId], references: [id], onDelete: Cascade, onUpdate: Cascade)
listC C[]
}
model C {
id Int @id @default(autoincrement())
col1 String @db.VarChar
col2 String @db.VarChar
cbId Int?
bData B? @relation(fields: [cbId], references: [id], onDelete: Cascade, onUpdate: Cascade)
listD D[]
}
model D {
id Int @id @default(autoincrement())
col1 String @db.VarChar
col2 String @db.VarChar
dcId Int?
cData C? @relation(fields: [dcId], references: [id], onDelete: Cascade, onUpdate: Cascade)
} import { Prisma, PrismaClient } from '@prisma/client'
export const db = new PrismaClient()
const selectA: Prisma.ASelect = {
id: true,
col1: true,
col2: true
}
const selectB: Prisma.BSelect = {
id: true,
col1: true,
col2: true,
aData: {
select: selectA
}
}
const selectC: Prisma.CSelect = {
id: true,
col1: true,
col2: true,
bData: {
select: selectB
}
}
const query1 = await db.c.findMany({
select: selectC
})
const result1 = query1[0].bData.aData
const result2 = query1[0].bData?.baId
const query2 = await db.c.findMany({
select: {
id: true,
col1: true,
col2: true,
bData: {
select: {
id: true,
col1: true,
col2: true,
aData: {
select: {
id: true,
col1: true,
col2: true
}
}
}
}
}
})
const result3 = query2[0].bData?.aData
const result4 = query2[0].bData?.baId Environment & setup
Prisma Version
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
After running the code, I'm confirm this is only typescript that throwing the error. While the resulting endpoint work normally. |
Beta Was this translation helpful? Give feedback.
-
Hi @ashlite To get what you try to achieve you need to remove type annotation and use const selectB = {
id: true,
col1: true,
col2: true,
aData: {
select: selectA
}
} as const; If you still want make sure it's valid const selectB = {
id: true,
col1: true,
col2: true,
aData: {
select: selectA
}
} as const satisfies Prisma.BSelect; |
Beta Was this translation helpful? Give feedback.
Hi @ashlite
In order for TS inference to work, we need a precise type for select parameters. Since your variables are annotated with generic
ASelect
andBSelect
types this would be the type the variable would have. TS will not know about specific fields and values stored in there anymore, it will only know that it can be any value that select can accept. Therefore, inference for result types will no longer work.To get what you try to achieve you need to remove type annotation and use
as const
modifier:If you still want make sure it's valid
SelectB
input while also preserving the exac…