Search code examples
vaporvapor-fluent

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


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?


Solution

  • You can do something like

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