57

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?

3
  • Is it already in production? Otherwise you can simply rollback and manually remove the default. Commented Apr 16, 2014 at 5:21
  • @rails4guides.com that's what I ended up doing. I thought there might be a better way to do it. Commented Apr 16, 2014 at 23:55
  • possible duplicate of Correct Way to Set Default Values in Rails Commented Sep 23, 2014 at 13:10

4 Answers 4

68

From the docs:

  def up
    change_column_default :table_name, :status, 0
  end

  def down
    change_column_default :table_name, :status, nil
  end
Sign up to request clarification or add additional context in comments.

2 Comments

I think you meant to write :table_name instead of :column_name for that first value. change_column_default(table_name, column_name, default) public
I believe OP wants to remove the not null constraint... rather than changing a default value. They only mentioned change_column_default as something they tried, not knowing that that isn't the method they need to use.
38

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

4 Comments

Can you add an explanation as well, what is the difference to this answer?
that answer refers changing the default value of a column. my answer sets whether this column allow null value or not.
@TobiasLiefke because this answer is the only correct answer, even 7 years later. It solves the problem, removing NOT NULL constraint, which has nothing to do with column defaults
@RyanRomanchuk I added my comment when there was no text at all. If I remove it now, ran632s comment would make less sense. And the answer still lacks the information, which you just have given. See the answers from ddebruler and Rick Smith for an example: Always explain, why this code solves the problem.
21

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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.