Hello all, Is there a way I can alter table?. For ...
# orm-help
j
Hello all, Is there a way I can alter table?. For eg I have 10 users with 3 fields. Lets say a fourth field is required for user keep value null for existing users. Changing model throws me an error. I don't want to purge my db and start from scratch. How can I achieve this?
r
Could you share your model and how did you add the new field?
j
model User {
id Int @id @default(autoincrement())
name  String
email String @unique
}
Let's say I create x users with this model.Now want to add some other field like rating or something. So updated model to
model User {
id Int @id @default(autoincrement())
name  String
email String @unique
rating Int
}
when I try migration it throws me error
r
Copy code
model User {
  id Int @id @default(autoincrement())
  name  String
  email String @unique
  rating Int?
}
Try this
💯 1
j
Yea. Will do 👍
r
The
?
means that the field is nullable
👍 1
j
It is working. Thanks a lot.
💯 1
m
@Ryan What if the rating field is a required field!? Do you do rating Int? and later rating Int ?
r
If rating is required, then you need to do these steps: 1. add rating Int? 2. fill the previous records first with a rating so that there are no constraint violations 3. convert it to rating Int
👍 1
r
ahaa - I was wondering about that, thanks!