Rails執行資料庫復原時報錯:ActiveRecord::IrreversibleMigration exception,exceptional

來源:互聯網
上載者:User

Rails執行資料庫復原時報錯:ActiveRecord::IrreversibleMigration exception,exceptional
最近在rails3.2下修改資料庫表的欄位,然後想復原取消操作,但是在執行rake db:rollback命令時,出現錯誤:

rake aborted!An error has 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'

我的migration內容如下:

class ChangeVmTempColumns < ActiveRecord::Migration  def change    change_table :vm_temps do |t|      t.change :disksize, :integer, :limit => 8      t.change :mem_total, :integer, :limit => 8    end  endend

上網查了資料,貌似原因在於如果在migration中做的資料類型轉換是破壞性的時,就不能完成復原。也就是說,對資料庫表的欄位類型進行修改時,資料庫中的資料也會有變化,這樣不能復原這些變動的資料。

《The migration that cannot be undone: Irreversible Migration》文章中舉了一個例子:當我們在migration中change_column由integer變為string時是可以的,但是如果反過來,欄位類型由string變為integer,我們就不能reverse this migration。正好和我這種情況一致!

Stackoverflow上,這個問題《ActiveRecord::IrreversibleMigration exception when reverting migration》提供了一個解決辦法:把self.change改為self.up和self.down方法。

修改後的migration:

class ChangeVmTempColumns < ActiveRecord::Migration  def self.up    change_table :vm_temps do |t|      t.change :disksize, :integer, :limit => 8      t.change :mem_total, :integer, :limit => 8    end  end  def self.up    change_table :vm_temps do |t|      t.change :disksize, :string      t.change :mem_total, :string    end  endend

執行rake db:rollback,成功!

原因:我原來認為在Rails中,self.change方法直接把self.up和self.down兩個綜合在一起,執行和復原只用一個change方法就可以,但是經過這個例子,我認為self.change方法執行復原時,只能採用預設的方式執行,一旦出現上述類型轉換的問題就無法正常執行;但是self.down方法執行復原時,會強制執行self.down中的語句,這樣就不會出現irreversible migration的錯誤。


Ralis串連遠端資料庫,提示ActiveRecord::ConnectionNotEstablished ,本地pl/sql可以訪問遠端資料庫

你是區域網路內可以遠端連線,廣域網路使用外網ip不可以串連嗎?
路由器上要映射ip地址和資料庫連接埠
 
在ruby指令碼中ActiveRecord::Baseestablish_connection怎串連2個資料庫

一個connection只能串連一個資料庫
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.