101

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

Pykih
  • 2,769
  • 3
  • 29
  • 38
  • 1
    possible duplicate of [How to change a nullable column to not nullable in a Rails migration?](http://stackoverflow.com/questions/5966840/how-to-change-a-nullable-column-to-not-nullable-in-a-rails-migration) – laffuste Mar 10 '14 at 14:53

2 Answers2

142

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.

Undo
  • 25,519
  • 37
  • 106
  • 129
user1309272
  • 1,450
  • 1
  • 9
  • 4
  • 13
    I'd recommend using `change_column_default` along with this if you want `null` to be the default column value. Otherwise it will be `false` or `0`. – Joshua Pinter Nov 08 '15 at 17:38
115

Try:

change_column :table_name, :email, :string, null: true
shilovk
  • 11,718
  • 17
  • 75
  • 74
Pratik Khadloya
  • 12,509
  • 11
  • 81
  • 106