我是Ruby on Rails的新手,非常感谢有关迁移的一些帮助。 第4版
首次迁移
class CreateRooms < ActiveRecord::Migration
def change
create_table :rooms do |t|
t.integer :legacy_id, null: true
t.string :tag, null: false
t.string :name_en, null: false
t.string :name_et, null: false
t.string :name_ru, null: false
t.string :color, default: 'Black'
t.timestamp :enabled_from, default: 0
t.timestamp :enabled_to, default: 0
t.timestamps null: false
end
add_index :rooms, :tag, unique: true
add_index :rooms, :name_en, unique: true
add_index :rooms, :name_et, unique: true
add_index :rooms, :name_ru, unique: true
end
end
下一个
class AddDefaultValuesToRoom < ActiveRecord::Migration
def change
add_column :rooms, :priority, :integer, null: false
change_column :rooms, :enabled_from, :datetime, default: '2008-08-15'
change_column :rooms, :enabled_to, :datetime, default: '2050-12-31'
add_index :rooms, :priority, unique: true
end
end
奇怪的是,这并没有反映在架构中
create_table "rooms", force: :cascade do |t|
t.integer "legacy_id"
t.string "tag", null: false
t.string "name_en", null: false
t.string "name_et", null: false
t.string "name_ru", null: false
t.string "color", default: "Black"
t.datetime "enabled_from", default: '2008-08-15 00:00:00'
t.datetime "enabled_to", default: '2050-12-31 00:00:00'
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "rooms", ["name_en"], name: "index_rooms_on_name_en", unique: true
add_index "rooms", ["name_et"], name: "index_rooms_on_name_et", unique: true
add_index "rooms", ["name_ru"], name: "index_rooms_on_name_ru", unique: true
add_index "rooms", ["tag"], name: "index_rooms_on_tag", unique: true
rake db:rollback崩溃
rake db:rollback STEP=2 --trace
** Invoke db:rollback (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:rollback
ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
Migrating to AddDefaultValuesToRoom (20160127121254)
(0.1ms) begin transaction
== 20160127121254 AddDefaultValuesToRoom: reverting ===========================
(0.1ms) rollback transaction
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
ActiveRecord::IrreversibleMigration/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.2/lib/active_record/migration/command_recorder.rb:65:in `inverse_of'
现在我正在解决问题,有人可以帮助我吗?
TIA 帕维尔
答案 0 :(得分:0)
change_column
会对您的数据库进行不可逆转的更改,这意味着您无法将其回滚。请参阅指南:http://guides.rubyonrails.org/active_record_migrations.html#changing-columns
这里的想法是恢复数据会导致数据丢失,因此Rails会阻止您在默认情况下执行破坏性操作并抛出错误来停止进程。
更改上次迁移以使用reversible
或up
/ down
方法,以明确告知Rails如何撤消您的更改(请注意,您将丢失存储在新专栏priority
)。请参阅:http://guides.rubyonrails.org/active_record_migrations.html#using-reversible
以下是使用up
/ down
方法的示例:
class AddDefaultValuesToRoom < ActiveRecord::Migration
def up
add_column :rooms, :priority, :integer, null: false
change_column :rooms, :enabled_from, :datetime, default: '2008-08-15'
change_column :rooms, :enabled_to, :datetime, default: '2050-12-31'
add_index :rooms, :priority, unique: true
end
def down
remove_column :rooms, :priority
change_column :rooms, :enabled_from, :datetime, default: 0
change_column :rooms, :enabled_to, :datetime, default: 0
end
end