#Ts Error for Prisma Json field with null value
69 messages · Page 1 of 1 (latest)
could you please attach your error screenshots here?
There's a difference between null and Prisma.JsonNull.
with your schema, set your data as attribute:null means 'on database, attribute column has no value'(= db null, like you schema's 'name' column can have null value).
but set your data as attribute:Prisma.JsonNull means 'on datbase, attribute column has value, with json value null'.
and i suggested below codes to work on your case:
const user = await prisma.user.create({
data: {
...data,
attribute: data.attribute === null ? Prisma.JsonNull : data.attribute,
},
});
Read this docs can help your understanding much more: https://www.prisma.io/docs/orm/prisma-client/special-fields-and-types/working-with-json-fields#using-null-values
did u test my code?
typescript null means 'databse Null value(no data)'
and typescript Prisma.JsonNull means `database has value, with JSON value 'null'
Imported User type has attribute property, which JsonValue type.
JsonValue type is string | number | boolean | JsonObject | JsonArray | null.
Your schema defines attribute as Json , which means 'Not Nullable on database'.
so your error happens because attribute could not be DB Null value.
this image means 'i want to set attribute column to DB null'
but this image means 'i want to set attribute column to JSON value null '
@upbeat gate i hope u can understand this behavior.
No. to allow DB null value you have to set as this image
it means json null
There is no way to distinguish whether a null value in typescript means ‘DB null’ or ‘JSON null’
yes
bro
if set to this, can you tell whether the null value marked in red in the image above means ‘DB null’ or ‘JSON null’?
so we have to clarify which case of null we intended to
when create/update/upsert data
of course filtering data
no.. unfortunately typescript(and prisma) can't know which null data user intended to
if your schema defines attribute as Non-Nullable Json value
only
yes
yes that is
yes
hmm... type User is 'returned value'
user.attribute can have null value
but prisma can't know if this value is DB null or Json Null
even though DB nulls are not allowed in Prisma, it seems that the distinction needs to be made explicitly.
u can create new issue of this problem i think
your explanation also makes a sense
since Prisma is currently implemented that way, it seems inevitable
how about change title to Ts Error for Prisma Json field with null value ? lol
but i think create new issue of this problem on github can be more helpful
idk lol im not related on Prisma
yes it can be
JsonValue is 'return type' of Prisma Json field
if u call prisma.user.findFirst() or something, that data's attribute type is JsonValue .
which can be null in javascript
it is return type of User model
and you did receive arg as User model
but if u want to create User, u should receive this type of args
or this
you can use as this:
const createUser = async (data: Parameters<typeof prisma.user.create>[0]["data"]) => {
const user = await prisma.user.create({ data });
};
lol
yup
hmm...
in Prisma, it would be good to understand that the type used when creating and the result type of the query may be different.
especially if there is a Json type lol
you are trying to 'create user'
right?
on create, you need to set 'attribute' field to appropriate 'create type of User.attribute'
which means 'should not be typescript null'
yes
so, if u intend to create user with attribute 'JSON null', you should convert typescript null value to Prisma.JsonNull.
otherwise, just insert as-is.
JsonValue is 'return type', not 'create type' (of User.attribute).
yes i agree that in your case(not allow DB null, but can be JSON null), prisma should accept null value as JSON null smoothly
but current Prisma does not..lol
use this code
const user = await prisma.user.create({
data: {
...data,
attribute: data.attribute === null ? Prisma.JsonNull : data.attribute,
},
});
as i said
u need to convert return type to create type to match null value
if ur not using Json field, u can input as-is
but type problem is happened when ur using Json field lol
lol