prisma: After upgrading to 3.0.2, can't create or update records when json field is null
I’m having a similar error to https://github.com/prisma/prisma/issues/9199 after upgrading to Prisma 3.0.2 (from 2.x)
I have also ran prisma migrate with no changes in order to cohere with Upgrade Path for referential actions and named constraints
datamodel in particualar:
model Room {
id String @id @default(cuid()) @db.VarChar(30)
name String
updatedAt DateTime @updatedAt
createdAt DateTime @default(now())
accountId String @map("account") @db.VarChar(30)
bgColor Json?
account Account @relation(fields: [accountId], references: [id])
bookings Booking[]
@@index([accountId])
}
Mutation from Nexus:
t.field('createRoom', {
type: 'Room',
args: {
name: nonNull(stringArg()),
accountId: nonNull(idArg()),
bgColor: arg({type: "Json"}),
},
resolve: (_parent, args, ctx) => ctx.prisma.room.create({
data: {
...args
}
})
});
Error when bgColor is null (the same thing when creating or updating):
Invalid `prisma.room.create()` invocation: { data: { name: 'Test Room', accountId: 'ckm7v6zpi00130huil8hbwvlk', bgColor: null ~~~~ } } Argument bgColor for data.bgColor must not be null. Please use undefined instead.```
_Originally posted by @chemicalkosek in https://github.com/prisma/prisma/issues/9199#issuecomment-919430264_
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 1
- Comments: 19 (6 by maintainers)
That would mean rewriting even the simplest resolvers such as
to check for json null values. Can we make a default that will transform the null value to the previous behaviour? Or such thing will not be possible?
Hey everyone, sorry for the confusion here. This is expected, but we missed this in our documentation. We stopped allowing nulls for JSON field inputs because:
The above code is ambiguous:
The solution is to use
DbNullandJsonNull. To insert anullvalue into a Json field, you would write:And to insert a database
NULLinto a Json field, you would write:We’re going to improve this error message and also fix up our documentation.
Hi, I’m also running into the same issue. I’m doing:
And am receiving the error message:
However, if I understood things correctly, I’m not sure that this can be quite right:
null, I want to set a MySQLNULLvalue, regardless of what was there beforeundefined, I think that would let the value unchanged from whatever it was beforeIs that a correct understanding? If so I think this is a genuine bug, and not an expected change?
Would be nice to have a simple option that converts nulls to DbNulls automatically.
I’d say that a good spot would be in “Null and undefined” section, as an explanation that e.g. “for data consistency purposes, Prisma utilises helper types, such as DbNull or JsonNull”. That’d explain, why
not: nullis misbehaving with Json type fields. Of course now that I’ve localised the exact explanation as mentioned, I can’t say it’s nonexistent - nevertheless, it would be of great help to put Json typenull-ish values near the null-related topic.Hello! Can it be mentioned somewhere in the docs? I’ve stumbled upon this, since I’ve been looking for:
on a Json type field, and Prisma was giving me errors. The solution was:
Not sure if it’s mentioned, since my Googling didn’t show anything of the sort. Thanks in advance!
Edit: nevermind, just found it
here is a handy function to convert all
nulltoDbNullif that can helpI don’t think it is expected, we’ve been through it already in the linked issue. I’m not doing any filtering by Json. It’s about creating or updating a record with a null Json field value in the args