# Write Reversible Migration To Set Default

You can use the `change_column_default` method to alter the default value of a column. If the column doesn't have a default, then you'd essentially be changing the default from `nil` to *some value*.

```ruby
def up
  change_column_default :books, :published, false
end

def down
  change_column_default :books, :published, nil
end
```

This is fine, but you can write the migration as a single, reversible `change` method using the `:from` and `:to` options.

```ruby
def change
  change_column_default :books, :published, from: nil, to: false
end
```

When you migrate, the default will be set to `false`. When you rollback, the default will be removed.

[source](https://blog.arkency.com/how-to-add-a-default-value-to-an-existing-column-in-a-rails-migration/)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ploegert.gitbook.io/til/programmy/rails/write-reversible-migration-to-set-default.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
