Spreading data object with foreing keys in update() does not allow unchecked input
Basically the title summarizes my problem.
We have a model that has category relation so it has categoryId as a foreign key. We handle type safety and validation using typebox and then spread the input object in update's
data
object.
Like this:
Input contains flat parameters like strings and numbers and those get updated correctly but when we supply categoryId we get the following error:
The workaround was that we destruct the input and manually enter the categoryId and then spread the rest like this:
Why is this necessary? Why can't we just spread the whole object? Prisma just automatically expects the "not unchecked" input.1 Reply
You chose to compile with the bot. One moment while I piece together your answer…
This issue occurs because Prisma automatically switches between regular input types and unchecked input types based on the data you provide. When you spread the entire object with
categoryId
, Prisma tries to use the regular input type (which expects category: { connect: { id } }
format) but then encounters the direct categoryId
field which is only valid in the unchecked input type.
The behavior you're experiencing is related to how Prisma implements the XOR (exclusive OR) type between regular and unchecked inputs. When Prisma detects certain fields or patterns, it decides which input type to use, and if it can't match all fields to one type, it produces this error.
As one user reported, "if one of the fields is using the format of postId instead of post:{connect:{id}}
then it will automatically switch into unchecked mode. Once it switched into unchecked mode, then we're not able to use the connect
checked method anymore."
Your workaround is correct - by explicitly providing categoryId
separately from the spread, you're ensuring Prisma uses the unchecked input type consistently.
Another approach would be to transform your input to use the relational syntax instead:
This is a known limitation with Prisma's input type system. As explained by a Prisma contributor, allowing both syntaxes simultaneously would create too many permutations of input types, causing the input generation to "blow exponentially."
GitHub: 5788, 4322
✅ Great, thanks for letting us know that this solved your question!