문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top