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