Setting a default value for a new required field in Fluent migration

552 views Asked by At

I'm trying to add a new required field in a Fluent migration. Just like this example from the docs:

database.schema("planets")
    .field("name", .string, .required)
    .update()

But when I run migrate I get an error saying that column "name" of relation "planets" contains null values.

Is there a way to set the default value for adding a required field to a table with pre-existing records?

1

There are 1 answers

2
0xTim On BEST ANSWER

You can do something like

database.schema("planets")
    .field("name", .string, .required, .sql(.default("Unknown"))
    .update()