adding required fields to a prisma table while having data

This is probably a very common issue you might've been facing. Let's say I've a table user in prisma
model User {
id Int @id() @default(autoincrement())
name String @unique
}
model User {
id Int @id() @default(autoincrement())
name String @unique
}
Now this is the table i have on prod. Later on , i want to add email field and it want it to be required. But if i try to migrate that , prisma will ask me to reset the db. Is there any workaround this , without having to make the email field optional?
4 Replies
Brendonovich
Brendonovich•2y ago
Not really - Prisma is entirely correct. If you really want to do it then 1. Add it as an optional field, 2. Fill in values for existing rows in some way, 3. Make it required now that every row has a value
Pluto
Pluto•2y ago
So if i migrate it as optional , then add values to all the existing rows and change it to required , prisma will allow that?
Brendonovich
Brendonovich•2y ago
Should do
Paweł
Paweł•2y ago
Hey @Pluto can you give feedback if it worked? 😄 I'm quite curious : )
Want results from more Discord servers?
Add your server