Question

My schema:

  create_table "location_hours", force: true do |t|
    t.datetime "start_at"
    t.datetime "end_at"
    t.integer  "location_id"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

My migration:

class ChangeLocationHourNulls < ActiveRecord::Migration
  def change
    change_column :location_hours, :start_at, :datetime, :null => :false
    change_column :location_hours, :end_at, :datetime, :null => :false
    change_column :location_hours, :location_id, :integer, :null => :false
  end
end

Rake Output:

$ bundle exec rake db:migrate
[deprecated] I18n.enforce_available_locales will default to true in the future. If you really want to skip validation of your locale you can set I18n.enforce_available_locales = false to avoid this message.
==  ChangeLocationHourNulls: migrating =======================================
-- change_column(:location_hours, :start_at, :datetime, {:null=>:false})
   -> 0.0008s
-- change_column(:location_hours, :end_at, :datetime, {:null=>:false})
   -> 0.0006s
-- change_column(:location_hours, :location_id, :integer, {:null=>:false})
   -> 0.0032s
==  ChangeLocationHourNulls: migrated (0.0067s) ==============================

   -> 0.0032s
==  ChangeLocationHourNulls: migrated (0.0067s) ==============================

When I check my schema file, it hasn't changed, and the database hasn't changed. Any ideas on what could cause this?

Was it helpful?

Solution

Rollback the migration ChangeLocationHourNulls.

Then change your migration as below:

class ChangeLocationHourNulls < ActiveRecord::Migration
  def change
    change_column :location_hours, :start_at, :datetime, :null => false
    change_column :location_hours, :end_at, :datetime, :null => false
    change_column :location_hours, :location_id, :integer, :null => false
  end
end

Use false and not :false.

Run rake db:migrate

OTHER TIPS

I believe @Kirti advise should solve the issue but i just realized there is a better option in your case, since you just want to change Nullable option:

You could use change_column_null which works like this:

class ChangeLocationHourNulls < ActiveRecord::Migration
  def change
    change_column_null :location_hours, :start_at, false
    change_column_null :location_hours, :end_at, false
    change_column_null :location_hours, :location_id, false
  end
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top