P
Prisma2mo ago
kartîk

Ts Error for Prisma Json field with null value

No description
No description
89 Replies
kartîk
kartîk2mo ago
@Kuhave…can you explain why :user will not work even if i m importing it from client only
Kuhave
Kuhave2mo ago
could you please attach your error screenshots here?
Kuhave
Kuhave2mo 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îk2mo ago
No description
kartîk
kartîk2mo 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
Kuhave2mo 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
Kuhave2mo ago
this image means 'i want to set attribute column to DB null'
No description
Kuhave
Kuhave2mo ago
but this image means 'i want to set attribute column to JSON value null '
No description
Kuhave
Kuhave2mo ago
@kartîk i hope u can understand this behavior.
kartîk
kartîk2mo 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
Kuhave2mo ago
No. to allow DB null value you have to set as this image
No description
Kuhave
Kuhave2mo ago
it means json null
kartîk
kartîk2mo ago
I dont want to allow null Values
Kuhave
Kuhave2mo ago
There is no way to distinguish whether a null value in typescript means ‘DB null’ or ‘JSON null’
kartîk
kartîk2mo ago
So the thing is as per my schema my attribute can have json null right?
Want results from more Discord servers?
Add your server