How to deal with the error `Error Cannot return nu...
# prisma1-community
s
How to deal with the error
Error Cannot return null for non-nullable type
. I added a new attribute 
name!
  in the data model file and then did
prisma generate
and
prisma deploy
. When I query, it gives the above error
r
Hey @Suhail 👋 Could you share your datamodel and query that you are performing?
s
Initial data model
type User {id: ID! @id, username: string}
Then I did
prisma generate
and then
prisma deploy
I performed insertions and update queries using the API generated by prisma
prisma.updateUser
Then I updated the data model file as
type User {id: ID! @id, username: string, name: string!}
adding
name
attribute and making it
!
. Now when I perform
prisma.user
(i.e. get query), I get this error @Ryan
r
You have added a non-nullable field
name
. Due to this the previous records will have the
name
field set to
null
. Could you try adding non-null values to name for all the current records in the database and then query again?
s
Yes, that will work. But is there any other way out? I am working with a Mongo database and if this happens in prod, I will have to update all the documents in a collection @Ryan
r
You would need to add a nullable field then like:
name: String
If you add a non-null field, then you would always need to update the previous records before performing the migrations.
s
Alright. Is there a way with
prisma
to do automatic migrations?
r
Could you explain what automatic would be in this case? Do you want to perform any operation before/after migrations?
s
Yes. Considering the above case. Would there be a way where I could add non-null [empty string] values before doing
prisma deploy
?
r
It would have to be done in the following steps: 1. Create a field first that is nullable:
name: String
and run
prisma deploy
2. Seed the data via a script (either js/ts) file into all current records in the database 3. Change the field to non-nullable
name: String!
and again run
prisma deploy
Currently you would have to follow these steps to add a non-nullable field with already present records
s
Okay