Add a default value to a column through a migration

Jon picture Jon · Aug 17, 2011 · Viewed 166.7k times · Source

How do I add a default value to a column that already exists through a migration?

All the documentation I can find shows you how to do it if the column doesn't already exist but in this case it does.

Answer

Maurício Linhares picture Maurício Linhares · Aug 17, 2011

Here's how you should do it:

change_column :users, :admin, :boolean, :default => false

But some databases, like PostgreSQL, will not update the field for rows previously created, so make sure you update the field manaully on the migration too.