The field of the database table was recently modified under rails3.2, and then you want to roll back the cancel operation, but an error occurred while executing the rake db:rollback command:
Rake aborted! An error had occurred, all later migrations canceled:activerecord::irreversiblemigration/usr/local/rvm/gems/ Ruby-1.9.3-p392/gems/activerecord-3.2.14/lib/active_record/migration/command_recorder.rb:42:in ' Block in inverse '
My migration content is as follows:
Class Changevmtempcolumns < activerecord::migration def change change_table:vm_temps do |t| T.change:d isksize,: integer,: limit = 8 t.change:mem_total,: integer,: limit = 8 end EndEnd
Check the data on the Internet, it seems that the reason is that when you modify the field type of the database table, the data in the database will also change, so that the changes can not be modified in the rollback of the data
"The migration that cannot is undone:irreversible migration" article cited an example: when we migration in Change_ It is possible to change the column from integer to string, but if the field type is changed from string to Integer, we cannot reverse this migration. It's just the same with me!
StackOverflow, this question "activerecord::irreversiblemigration exception when reverting migration" Provides a solution: Change the Self.change to Self.up and Self.down methods.
Modified migration:
Class Changevmtempcolumns < activerecord::migration def self.up change_table:vm_temps do |t| T.change:d isksize,: integer,: limit = 8 t.change:mem_total,: integer,: limit = 8 end end def SE Lf.up change_table:vm_temps do |t| T.change:d isksize,: String t.change:mem_total,: string end EndEnd
Execute rake db:rollback, Success!
Reason: I originally thought that in rails, Self.change method directly to the Self.up and Self.down two together, execution and rollback only with a change method can be, but after this example, I think the Self.change method execution rollback, can only be executed by default, once the above type conversion problem can not be Normal execution, but when the Self.down method performs a rollback, the statements in Self.down are enforced so that no irreversible migration errors occur.
Rails performs database rollback times error: Activerecord::irreversiblemigration exception