我的架构:
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
我的迁移:
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
耙子输出:
$ 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) ==============================
当我检查我的架构文件时,它没有改变,数据库也没有改变。关于可能导致这种情况的任何想法?
答案 0 :(得分:2)
回滚迁移ChangeLocationHourNulls
。
然后按以下方式更改您的迁移:
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
使用false
而非:false
。
运行rake db:migrate
答案 1 :(得分:0)
我相信@Kirti建议应该解决这个问题,但我刚刚意识到你的情况有更好的选择,因为你只想改变Nullable选项:
您可以使用change_column_null,其工作原理如下:
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