I had originally created a table with column as
t.string "email", :default => "", :null => false
The requirement has changed and now I need to allow email to be null. How can I write a migration to make :null => true
According to the documentation one of these options is :null which allows or disallows NULL values in the column. So, in summary :null => false would mean "Do not allow NULL values in the new or update column".
You must rollback the migration (for example with bin/rails db:rollback ), edit your migration, and then run bin/rails db:migrate to run the corrected version.
To run a specific migration up or down, use db:migrate:up or db:migrate:down . The version number in the above commands is the numeric prefix in the migration's filename. For example, to migrate to the migration 20160515085959_add_name_to_users. rb , you would use 20160515085959 as the version number.
A Rails migration is a tool for changing an application's database schema. Instead of managing SQL scripts, you define database changes in a domain-specific language (DSL). The code is database-independent, so you can easily move your app to a new platform.
I could not get the above solution to work with Active Record 4.0.8 and Postgresql 9.3
However change_column_null worked perfectly.
change_column_null :users, :email, true
The reverse has a nice option to update existing records (but not set the default) when null is not allowed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With