Error when setting a relation field to null
#459
Comments
Thanks for reporting this! I can indeed confirm this behaviour. We should match the typing with the behaviour or we should allow nulls for connects. Either way this is a confirmed bug now. More minimal reproduction: datasource db {
provider = "sqlite" // other options are: "mysql" and "sqlite"
url = "sqlite:dev.db"
}
generator client {
provider = "prisma-client-js"
}
model User {
id String @id @default(cuid())
name String
posts Post[]
}
model Post {
id String @id @default(cuid())
title String
content String
user User
} import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
async function main() {
const data = await prisma.user.create({
data: {
name: "Harshit Pant",
posts: {
connect: null
}
}
});
console.log(data);
}
main(); |
Thanks a lot for reporting! I just looked into it and there is an important difference between the reported error and the reproduction. The reported error fails in const data = await prisma.user.create({
data: {
name: "Harshit Pant",
posts: null
}
}); Errors in
Whereas the reproduction mentions const data = await prisma.user.create({
data: {
name: "Harshit Pant",
posts: { connect: null }
}
}); And errors in
They are related to different problems, but of course both need to be fixed. |
Related, and probably resolved: #614 |
I can confirm this has been fixed in the latest alpha. @kgoggin Can you please confirm, to install the alpha use |
@pantharshit00 sorry I can't easily confirm - I've since refactored that code and don't have an easy way to verify. Glad for the fix though! |
Hello! I just stumbled on this error that I believe to be a bug and thought I'd share it. I've got a call out to create an entity with an optional 1:1 relation to another entity. Here's the code:
The offending line is where I'm setting the
subject
field with a ternary statement. If it evaluates asnull
, then the call fails with the following error:The
code
field isP2009
if that's helpful.The workaround for me is to change
null
toundefined
, which seems to work fine. Note that TypeScript is cool with either value, so either the generated types are wrong or something with the query engine is.Thanks!
The text was updated successfully, but these errors were encountered: