Rails performs database rollback times error: Activerecord::irreversiblemigration exception

Source: Internet
Author: User

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

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.