P
Prismaâ€Ē2mo ago
vanHessler

Prisma Select TypeError

ok - my issue might be unrelated: This works:
function manyFoo(select?: Prisma.fooSelect) {
const builtSelect = {
...select,
id: true,
name: true,
Bar: {
select: {
id: true,
name: true,
Baz: {
select: {
id: true,
name: true
}
}
}
}
} as const satisfies Prisma.fooSelect;
// find many foo
const results = await prisma.foo.findMany({
select: builtSelect,
});
return results.map((foo) => {
return {
...foo,
bar: foo.Bar.name,
baz: foo.Bar.Baz.name,
};
});
}
function manyFoo(select?: Prisma.fooSelect) {
const builtSelect = {
...select,
id: true,
name: true,
Bar: {
select: {
id: true,
name: true,
Baz: {
select: {
id: true,
name: true
}
}
}
}
} as const satisfies Prisma.fooSelect;
// find many foo
const results = await prisma.foo.findMany({
select: builtSelect,
});
return results.map((foo) => {
return {
...foo,
bar: foo.Bar.name,
baz: foo.Bar.Baz.name,
};
});
}
This does not:
function manyFoo(select?: Prisma.fooSelect) {
const builtSelect = {
id: true,
name: true,
Bar: {
select: {
id: true,
name: true,
Baz: {
select: {
id: true,
name: true,
},
},
},
},
...select,
} as const satisfies Prisma.fooSelect;


// find many foo
const results = await prisma.foo.findMany({
select: builtSelect,
});
return results.map((foo) => {
return {
...foo,
bar: foo.Bar.name,
baz: foo.Bar.Baz.name, //<-- ERROR HERE
};
});
}
function manyFoo(select?: Prisma.fooSelect) {
const builtSelect = {
id: true,
name: true,
Bar: {
select: {
id: true,
name: true,
Baz: {
select: {
id: true,
name: true,
},
},
},
},
...select,
} as const satisfies Prisma.fooSelect;


// find many foo
const results = await prisma.foo.findMany({
select: builtSelect,
});
return results.map((foo) => {
return {
...foo,
bar: foo.Bar.name,
baz: foo.Bar.Baz.name, //<-- ERROR HERE
};
});
}
When i move the select param to the bottom after a multiple nested relation lookup, it throws errors for the nested relation's properties. Why?
5 Replies
Prisma AI Help
Prisma AI Helpâ€Ē2mo ago
You selected the carefully hand-crafted route. A dev artisan will respond soon. Meanwhile, the #ask-ai channel awaits if you're curious!
Nurul
Nurulâ€Ē2mo ago
Hey 👋 What happens if you do something like this?
function manyFoo(select?: Prisma.fooSelect) {
const baseSelect = {
id: true,
name: true,
Bar: {
select: {
id: true,
name: true,
Baz: {
select: {
id: true,
name: true,
},
},
},
}
};

const builtSelect = {
...baseSelect,
...select,
} as const satisfies Prisma.fooSelect;

const results = await prisma.foo.findMany({
select: builtSelect,
});

return results.map((foo) => ({
...foo,
bar: foo.Bar.name,
baz: foo.Bar.Baz.name,
}));
}
function manyFoo(select?: Prisma.fooSelect) {
const baseSelect = {
id: true,
name: true,
Bar: {
select: {
id: true,
name: true,
Baz: {
select: {
id: true,
name: true,
},
},
},
}
};

const builtSelect = {
...baseSelect,
...select,
} as const satisfies Prisma.fooSelect;

const results = await prisma.foo.findMany({
select: builtSelect,
});

return results.map((foo) => ({
...foo,
bar: foo.Bar.name,
baz: foo.Bar.Baz.name,
}));
}
Does the order of select still cause issues for you?
vanHessler
vanHesslerOPâ€Ē2mo ago
yes, that was one of the first things i tried. Still causes issues. Its something to do with the relation nested select bit. I can put my select using type declaration anywhere above the following bit and not have problems:
Bar: {
select: {
id: true,
name: true,
Baz: {
select: {
id: true,
name: true,
},
},
},
}
Bar: {
select: {
id: true,
name: true,
Baz: {
select: {
id: true,
name: true,
},
},
},
}
But if it goes anywhere after it, it causes errors in the Baz level of the results (specifically that property Baz on foo.Bar does not exist. If i put the select before that bit, no errors ðŸĪŠ
janglad
jangladâ€Ē2mo ago
is this not just because when spreading object keys only get merged on level deep (and so types do too?) if the second object can have select.Baz be undefined it doesn't matter if select.Baz.select is defined in the first object you spread, it will still be typed as optional so maybe missing smth but this makes sense to me 😄
vanHessler
vanHesslerOPâ€Ē5w ago
ohhhhh yes, I didn't even think about that. I appear to be having a brain fart. Thanks! Can mark this one closed or completed or w/e 🙂

Did you find this page helpful?