Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Migration to make a column null => true

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

like image 800
Pykih Avatar asked Jun 05 '12 16:06

Pykih


People also ask

What is null false in rails migration?

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".

Which command is true to rollback migration in rails?

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.

How do I run a specific migration in rails?

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.

What can rails migration do?

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.


1 Answers

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.

like image 141
user1309272 Avatar answered Oct 09 '22 04:10

user1309272