Recently rails3.2 is changing the database table field, and then you want to roll back the cancel, but run the rake db:rollback Command, Error:
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
On the Internet to check the data, it seems that the reason is to assume that the data type conversion in the migration is destructive, you cannot complete the rollback.
That is, when you make changes to the field type of a database table. The data in the database also changes so that the data of these changes cannot be rolled back.
"The migration that cannot is undone:irreversible migration" article cited a sample: when we migration in Change_ It is possible to change the column from integer to string, but assume the reverse. The field type is changed from string to Integer, so we cannot reverse this migration. It's exactly the same as mine!
StackOverflow, this question "activerecord::irreversiblemigration exception when reverting migration" Provides a workaround: Change the Self.change to the Self.up and Self.down methods.
Migration after the change:
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
Run Rake db:rollback, Success!
reason: I used to think that in rails, The Self.change method directly combines the Self.up and Self.down two, and the run and rollback is only possible with a change method, but after this example, I think the Self.change method runs rollback. Can only be run in the default way, once the above type of conversion problem will not work properly. However, the Self.down method runs rollback. The Self.down declaration is forced to run, so there is no irreversible migration error.
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Rails when you run a database rollback error: Activerecord::irreversiblemigration exception