Rails when you run a database rollback error: Activerecord::irreversiblemigration exception

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.