Rails Migration: Remove constraint

Jay Godse picture Jay Godse · Apr 15, 2011 · Viewed 48.5k times · Source

I have a table in a Rails application which (in schema.rb) looks like:

create_table "users", :force => true do |t|
   t.string "name", :null=>false
   t.string "address", :null=>false
end

I would like to write a rails migration to allow nulls for the address field. i.e. after the migration the table looks like this:

create_table "users", :force => true do |t|
   t.string "name", :null=>false
   t.string "address"
end

What do I need to do to remove the constraint?

Answer

deepak picture deepak · Jan 31, 2014

In Rails 4+ in order to remove not-null constraint, you can use change_column_null:

change_column_null :users, :address, true