Rails 4: Remove not null constraint from table column with migration?

Ruby on-Rails-4Rails Migrations

Ruby on-Rails-4 Problem Overview


Given the following schema.rb:

  create_table "people", force: true do |t|
    t.string   "name",  null: false
    t.integer  "age"
    t.integer  "height"
    t.string   "email"
    t.boolean  "married",  default: false
    t.text     "bio"
    t.integer  "fav_number"
    t.decimal  "lucky_num",  precision: 2, scale: 2
    t.datetime "birthday"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

I'd like to remove the name default value of null: false. I've tried running a separate migration with change_column_default, but that had no impact on schema.rb. Any suggestions?

Ruby on-Rails-4 Solutions


Solution 1 - Ruby on-Rails-4

From the docs:

  def up
    change_column_default :table_name, :status, 0
  end

  def down
    change_column_default :table_name, :status, nil
  end

Solution 2 - Ruby on-Rails-4

The 'up' function will definitely do the job when you do db:migrate.
But in the future, in some cases, like rollback, you might want a function to reverse this particular migration.

def up
  change_column_null :people, :name, true
end

def down
  change_column_null :people, :name, false
end

Solution 3 - Ruby on-Rails-4

It sounds like you're not trying to change the default value of the column, but rather to remove the NOT NULL constraint and allow null values (i.e. change from "null: false" to the default "null: true"). If that's the case, then you can use change_column_null:

class ChangeNameNull < ActiveRecord::Migration
  def change
    change_column_null :people, :name, true
  end
end

Edit 1:- Fixed typo

Solution 4 - Ruby on-Rails-4

def change
  change_column_null(:users, :admin, false, <put a default value here> )
  # change_column(:users, :admin, :string, :default => "")
end

Changing a column with NULL values in it to not allow NULL will cause problems. This is exactly the type of code that will work fine in your development setup and then crash when you try to deploy it to your LIVE production. You should first change NULL values to something valid and then disallow NULLs. The 4th value in change_column_null does exactly that. See documentation for more details.

Also, I generally prefer to set a default value for the field so I won't need to specify the field's value every time I create a new object. I included the commented out code to do that as well.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongabethegrapeView Question on Stackoverflow
Solution 1 - Ruby on-Rails-4luigi7upView Answer on Stackoverflow
Solution 2 - Ruby on-Rails-4ran632View Answer on Stackoverflow
Solution 3 - Ruby on-Rails-4ddebrulerView Answer on Stackoverflow
Solution 4 - Ruby on-Rails-4Rick SmithView Answer on Stackoverflow