P
Prisma6mo ago
kartîk

Ts Error for Prisma Json field with null value

No description
No description
89 Replies
kartîk
kartîkOP6mo ago
@Kuhave…can you explain why :user will not work even if i m importing it from client only
Kuhave
Kuhave6mo ago
could you please attach your error screenshots here?
Kuhave
Kuhave6mo 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.
kartîk
kartîkOP6mo ago
No description
kartîk
kartîkOP6mo 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
Kuhave6mo 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
Kuhave6mo ago
this image means 'i want to set attribute column to DB null'
No description
Kuhave
Kuhave6mo ago
but this image means 'i want to set attribute column to JSON value null '
No description
Kuhave
Kuhave6mo ago
@kartîk i hope u can understand this behavior.
kartîk
kartîkOP6mo 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
Kuhave6mo ago
No. to allow DB null value you have to set as this image
No description
Kuhave
Kuhave6mo ago
it means json null
kartîk
kartîkOP6mo ago
I dont want to allow null Values
Kuhave
Kuhave6mo ago
There is no way to distinguish whether a null value in typescript means ‘DB null’ or ‘JSON null’
kartîk
kartîkOP6mo ago
So the thing is as per my schema my attribute can have json null right?
Kuhave
Kuhave6mo ago
yes
kartîk
kartîkOP6mo ago
Yes and what is happening here
No description
kartîk
kartîkOP6mo ago
Here why it s complaining…? It s much confusing🥲
kartîk
kartîkOP6mo ago
If i m doing this
No description
kartîk
kartîkOP6mo ago
I m giving data:User It mean my data is according to User type right ?
Kuhave
Kuhave6mo ago
bro
Kuhave
Kuhave6mo ago
No description
Kuhave
Kuhave6mo 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
Kuhave6mo ago
so we have to clarify which case of null we intended to
No description
No description
Kuhave
Kuhave6mo ago
when create/update/upsert data of course filtering data
kartîk
kartîkOP6mo 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
Kuhave6mo ago
no.. unfortunately typescript(and prisma) can't know which null data user intended to
kartîk
kartîkOP6mo ago
Oh yess…now i got ittt
Kuhave
Kuhave6mo ago
if your schema defines attribute as Non-Nullable Json value
kartîk
kartîkOP6mo ago
So my schema accepting Json null
Kuhave
Kuhave6mo ago
only yes
kartîk
kartîkOP6mo ago
So in create i have to explicitly tell Attribute is JSONnull
Kuhave
Kuhave6mo ago
yes that is
kartîk
kartîkOP6mo ago
But see Here User having attribute: jsonvalue
Kuhave
Kuhave6mo ago
yes
kartîk
kartîkOP6mo 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
Kuhave6mo 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
kartîk
kartîkOP6mo 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
Kuhave6mo 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
kartîk
kartîkOP6mo ago
Yes right Still will wait someone else to comment on this
Kuhave
Kuhave6mo ago
how about change title to Ts Error for Prisma Json field with null value ? lol
kartîk
kartîkOP6mo ago
Lol yeah makes more sense
Kuhave
Kuhave6mo ago
but i think create new issue of this problem on github can be more helpful
kartîk
kartîkOP6mo 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
Kuhave6mo ago
idk lol im not related on Prisma
kartîk
kartîkOP6mo 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
Kuhave6mo ago
yes it can be
kartîk
kartîkOP6mo ago
No description
Kuhave
Kuhave6mo ago
No description
kartîk
kartîkOP6mo ago
Type 'jsonValue' is not assignable to type 'JsonNull | InputJsonValue'. See jsonvalue is not allowed as json null
Kuhave
Kuhave6mo ago
JsonValue is 'return type' of Prisma Json field if u call prisma.user.findFirst() or something, that data's attribute type is JsonValue .
kartîk
kartîkOP6mo ago
But i did attribute : Json thn why JsonValue is being the type
Kuhave
Kuhave6mo ago
which can be null in javascript
Kuhave
Kuhave6mo ago
No description
Kuhave
Kuhave6mo ago
it is return type of User model and you did receive arg as User model
Kuhave
Kuhave6mo ago
but if u want to create User, u should receive this type of args
No description
Kuhave
Kuhave6mo ago
or this
No description
kartîk
kartîkOP6mo ago
Ahhh This is hard
Kuhave
Kuhave6mo ago
you can use as this:
No description
Kuhave
Kuhave6mo 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
kartîk
kartîkOP6mo ago
Bro
Kuhave
Kuhave6mo ago
yup
kartîk
kartîkOP6mo ago
Alien syntax Please explain me What is this? Why prisma sending something else in return type of User
Kuhave
Kuhave6mo ago
No description
kartîk
kartîkOP6mo ago
I simply said in my schema it s JSON
Kuhave
Kuhave6mo 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.
kartîk
kartîkOP6mo ago
Oh got it This is something new for me
Kuhave
Kuhave6mo ago
especially if there is a Json type lol
kartîk
kartîkOP6mo ago
I didn’t know So in my whats actually happening
Kuhave
Kuhave6mo ago
you are trying to 'create user' right?
kartîk
kartîkOP6mo ago
Yes
Kuhave
Kuhave6mo ago
on create, you need to set 'attribute' field to appropriate 'create type of User.attribute'
kartîk
kartîkOP6mo ago
Yes
Kuhave
Kuhave6mo ago
which means 'should not be typescript null'
kartîk
kartîkOP6mo 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
Kuhave6mo ago
yes
kartîk
kartîkOP6mo ago
Which s return type Thn ?
Kuhave
Kuhave6mo 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.
kartîk
kartîkOP6mo ago
Why i need to create it as json null????? If Jsonvalue allowing Null ?
Kuhave
Kuhave6mo ago
JsonValue is 'return type', not 'create type' (of User.attribute).
kartîk
kartîkOP6mo 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
Kuhave6mo 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
kartîk
kartîkOP6mo 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
Kuhave6mo 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
kartîk
kartîkOP6mo ago
Yes it works fine
Kuhave
Kuhave6mo 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
kartîk
kartîkOP6mo ago
Ah this should be fixed ig
Kuhave
Kuhave6mo ago
lol
kartîk
kartîkOP6mo ago
@Jon Harrell can you help with this one?
Want results from more Discord servers?
Add your server