P
Prisma7mo ago
Kartik

Ts Error for Prisma Json field with null value

No description
No description
89 Replies
Kartik
KartikOP7mo ago
@Kuhave…can you explain why :user will not work even if i m importing it from client only
Kuhave
Kuhave7mo ago
could you please attach your error screenshots here?
Kuhave
Kuhave7mo ago
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,
},
});
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
Working with Json fields (Concepts) | Prisma Documentation
How to read, write, and filter by Json fields.
Kartik
KartikOP7mo ago
No description
Kartik
KartikOP7mo ago
@Kuhave i m still confused If i m giving : User Why it s having problem… I imported User from client only And client is generated by prisma
Kuhave
Kuhave7mo ago
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.
Kuhave
Kuhave7mo ago
this image means 'i want to set attribute column to DB null'
No description
Kuhave
Kuhave7mo ago
but this image means 'i want to set attribute column to JSON value null '
No description
Kuhave
Kuhave7mo ago
@kartîk i hope u can understand this behavior.
Kartik
KartikOP7mo ago
Yes this helps but tell me one thing I did attribute : json okay? So in my schema i m setting attribute as JSON…mean it can also have dbnull? JsonValue type is string | number | boolean | JsonObject | JsonArray | null. As per this? This null means json null? Or dbnull?
Kuhave
Kuhave7mo ago
No. to allow DB null value you have to set as this image
No description
Kuhave
Kuhave7mo ago
it means json null
Kartik
KartikOP7mo ago
I dont want to allow null Values
Kuhave
Kuhave7mo ago
There is no way to distinguish whether a null value in typescript means ‘DB null’ or ‘JSON null’
Kartik
KartikOP7mo ago
So the thing is as per my schema my attribute can have json null right?
Kuhave
Kuhave7mo ago
yes
Kartik
KartikOP7mo ago
Yes and what is happening here
No description
Kartik
KartikOP7mo ago
Here why it s complaining…? It s much confusing🥲
Kartik
KartikOP7mo ago
If i m doing this
No description
Kartik
KartikOP7mo ago
I m giving data:User It mean my data is according to User type right ?
Kuhave
Kuhave7mo ago
bro
Kuhave
Kuhave7mo ago
No description
Kuhave
Kuhave7mo ago
if set to this, can you tell whether the null value marked in red in the image above means ‘DB null’ or ‘JSON null’?
Kuhave
Kuhave7mo ago
so we have to clarify which case of null we intended to
No description
No description
Kuhave
Kuhave7mo ago
when create/update/upsert data of course filtering data
Kartik
KartikOP7mo ago
Yesss you r right But if i m doing data:User thn it means it s jsonnull Isnt it? Coz in my schema attribute is Json Typescript will know when i did data:user ….in function params I m confused i think
Kuhave
Kuhave7mo ago
no.. unfortunately typescript(and prisma) can't know which null data user intended to
Kartik
KartikOP7mo ago
Oh yess…now i got ittt
Kuhave
Kuhave7mo ago
if your schema defines attribute as Non-Nullable Json value
Kartik
KartikOP7mo ago
So my schema accepting Json null
Kuhave
Kuhave7mo ago
only yes
Kartik
KartikOP7mo ago
So in create i have to explicitly tell Attribute is JSONnull
Kuhave
Kuhave7mo ago
yes that is
Kartik
KartikOP7mo ago
But see Here User having attribute: jsonvalue
Kuhave
Kuhave7mo ago
yes
Kartik
KartikOP7mo ago
So my data will not be with attribute jsonvalue? Coz i gave reference Data => USER Like see my data knows that email will be string Name will be string Coz i did data:user Thn why attribute is not referring to the same type of Json value?
Kuhave
Kuhave7mo ago
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
Kartik
KartikOP7mo ago
But schema never allowed it to be null thn how Schema said attribute is JsonValue Thn it will also return Json value Which can never be null
Kuhave
Kuhave7mo ago
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
Kartik
KartikOP7mo ago
Yes right Still will wait someone else to comment on this
Kuhave
Kuhave7mo ago
how about change title to Ts Error for Prisma Json field with null value ? lol
Kartik
KartikOP7mo ago
Lol yeah makes more sense
Kuhave
Kuhave7mo ago
but i think create new issue of this problem on github can be more helpful
Kartik
KartikOP7mo ago
Yes sure will do it but i think lets wait sometime for someone comment on this Should i tag john? Is it allow to tag like that?
Kuhave
Kuhave7mo ago
idk lol im not related on Prisma
Kartik
KartikOP7mo ago
Lets wait Hey Kuhave JsonValue type is string | number | boolean | JsonObject | JsonArray | null. This means actuall Null value Not jsonnull Jsonvalue also can be just “null” As you were saying it s jsonnull
Kuhave
Kuhave7mo ago
yes it can be
Kartik
KartikOP7mo ago
No description
Kuhave
Kuhave7mo ago
No description
Kartik
KartikOP7mo ago
Type 'jsonValue' is not assignable to type 'JsonNull | InputJsonValue'. See jsonvalue is not allowed as json null
Kuhave
Kuhave7mo ago
JsonValue is 'return type' of Prisma Json field if u call prisma.user.findFirst() or something, that data's attribute type is JsonValue .
Kartik
KartikOP7mo ago
But i did attribute : Json thn why JsonValue is being the type
Kuhave
Kuhave7mo ago
which can be null in javascript
Kuhave
Kuhave7mo ago
No description
Kuhave
Kuhave7mo ago
it is return type of User model and you did receive arg as User model
Kuhave
Kuhave7mo ago
but if u want to create User, u should receive this type of args
No description
Kuhave
Kuhave7mo ago
or this
No description
Kartik
KartikOP7mo ago
Ahhh This is hard
Kuhave
Kuhave7mo ago
you can use as this:
No description
Kuhave
Kuhave7mo ago
const createUser = async (data: Parameters<typeof prisma.user.create>[0]["data"]) => {
const user = await prisma.user.create({ data });
};
const createUser = async (data: Parameters<typeof prisma.user.create>[0]["data"]) => {
const user = await prisma.user.create({ data });
};
lol
Kartik
KartikOP7mo ago
Bro
Kuhave
Kuhave7mo ago
yup
Kartik
KartikOP7mo ago
Alien syntax Please explain me What is this? Why prisma sending something else in return type of User
Kuhave
Kuhave7mo ago
No description
Kartik
KartikOP7mo ago
I simply said in my schema it s JSON
Kuhave
Kuhave7mo ago
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.
Kartik
KartikOP7mo ago
Oh got it This is something new for me
Kuhave
Kuhave7mo ago
especially if there is a Json type lol
Kartik
KartikOP7mo ago
I didn’t know So in my whats actually happening
Kuhave
Kuhave7mo ago
you are trying to 'create user' right?
Kartik
KartikOP7mo ago
Yes
Kuhave
Kuhave7mo ago
on create, you need to set 'attribute' field to appropriate 'create type of User.attribute'
Kartik
KartikOP7mo ago
Yes
Kuhave
Kuhave7mo ago
which means 'should not be typescript null'
Kartik
KartikOP7mo ago
The thing i specified in my schema for attribute doesn’t matter? I have to put it only what prisma is saying i think But the thing jsonvalue includes NULL
Kuhave
Kuhave7mo ago
yes
Kartik
KartikOP7mo ago
Which s return type Thn ?
Kuhave
Kuhave7mo ago
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.
Kartik
KartikOP7mo ago
Why i need to create it as json null????? If Jsonvalue allowing Null ?
Kuhave
Kuhave7mo ago
JsonValue is 'return type', not 'create type' (of User.attribute).
Kartik
KartikOP7mo ago
This is like…what s logic of providing schema if create type is not according to my schema ? Isnt it? I wrote prisma schema for being strict about what i m crating and adding to database Right Thn what is sense of not following schema?for creation
Kuhave
Kuhave7mo ago
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
Kartik
KartikOP7mo ago
If it s not allowing something Thn how it can send that same thing in Return type? He never allowed that thing and returning those types?
Kuhave
Kuhave7mo ago
use this code
const user = await prisma.user.create({
data: {
...data,
attribute: data.attribute === null ? Prisma.JsonNull : data.attribute,
},
});
const user = await prisma.user.create({
data: {
...data,
attribute: data.attribute === null ? Prisma.JsonNull : data.attribute,
},
});
as i said
Kartik
KartikOP7mo ago
Yes it works fine
Kuhave
Kuhave7mo ago
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
Kartik
KartikOP7mo ago
Ah this should be fixed ig
Kuhave
Kuhave7mo ago
lol
Kartik
KartikOP7mo ago
@Jon Harrell can you help with this one?

Did you find this page helpful?