Add a default value to a column through a migration
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.
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.
change_column_default :employees, :foreign, false
For Rails 4+, use change_column_default
def change
change_column_default :table, :column, value
end
Here's a link to change_column_default documentation.
链接地址: http://www.djcxy.com/p/47186.html上一篇: OO在Rails中的设计:在哪里放东西
下一篇: 通过迁移向列添加默认值