2017-08-02 70 views
1

這是我的第一個問題,所以請客氣一點。 我最近遇到這個問題。 添加列的遷移未正確回滾。 我總是可以用SQL語句來做到這一點,但我真的想弄清楚爲什麼這不起作用。在Rails 5.1中添加列遷移沒有正確回滾

我的遷移代碼看起來像這樣。

class ChangeTypeForColumnStatusForInvoices < ActiveRecord::Migration[5.1] 
    add_column :invoices, :tutor_pay, :integer 
    remove_column :invoices, :status, :integer 
    add_column :invoices, :status, :string 
end 

這裏是執行時的遷移和回滾。

jameshong ~/Projects/tad_is_my_boss/toptutoring $ rails db:migrate 
W, [2017-08-01T23:43:38.664232 #5782] WARN -- : [SKYLIGHT] [1.3.1] Running Skylight in development mode. No data will be reported until you deploy your app. 
(To disable this message for all local apps, run `skylight disable_dev_warning`.) 
-- add_column(:invoices, :tutor_pay, :integer) 
    -> 0.0030s 
-- remove_column(:invoices, :status, :integer) 
    -> 0.0015s 
-- add_column(:invoices, :status, :string) 
    -> 0.0011s 
== 20170801015229 ChangeTypeForColumnStatusForInvoices: migrating ============= 
== 20170801015229 ChangeTypeForColumnStatusForInvoices: migrated (0.0000s) ==== 

jameshong ~/Projects/tad_is_my_boss/toptutoring $ rails db:rollback 
W, [2017-08-01T23:44:00.076660 #5821] WARN -- : [SKYLIGHT] [1.3.1] Running Skylight in development mode. No data will be reported until you deploy your app. 
(To disable this message for all local apps, run `skylight disable_dev_warning`.) 
-- add_column(:invoices, :tutor_pay, :integer) 
-- add_column(:invoices, :tutor_pay, :integer) 
rails aborted! 
ActiveRecord::StatementInvalid: PG::DuplicateColumn: ERROR: column "tutor_pay" of relation "invoices" already exists 
: ALTER TABLE "invoices" ADD "tutor_pay" integer 
/Users/jameshong/Projects/tad_is_my_boss/toptutoring/db/migrate/20170801015229_change_type_for_column_status_for_invoices.rb:2:in `<class:ChangeTypeForColumnStatusForInvoices>' 
/Users/jameshong/Projects/tad_is_my_boss/toptutoring/db/migrate/20170801015229_change_type_for_column_status_for_invoices.rb:1:in `<top (required)>' 
bin/rails:4:in `require' 
bin/rails:4:in `<main>' 
PG::DuplicateColumn: ERROR: column "tutor_pay" of relation "invoices" already exists 
/Users/jameshong/Projects/tad_is_my_boss/toptutoring/db/migrate/20170801015229_change_type_for_column_status_for_invoices.rb:2:in `<class:ChangeTypeForColumnStatusForInvoices>' 
/Users/jameshong/Projects/tad_is_my_boss/toptutoring/db/migrate/20170801015229_change_type_for_column_status_for_invoices.rb:1:in `<top (required)>' 
bin/rails:4:in `require' 
bin/rails:4:in `<main>' 
ActiveRecord::StatementInvalid: PG::DuplicateColumn: ERROR: column "tutor_pay" of relation "invoices" already exists 
: ALTER TABLE "invoices" ADD "tutor_pay" integer 
/Users/jameshong/Projects/tad_is_my_boss/toptutoring/db/migrate/20170801015229_change_type_for_column_status_for_invoices.rb:2:in `<class:ChangeTypeForColumnStatusForInvoices>' 
/Users/jameshong/Projects/tad_is_my_boss/toptutoring/db/migrate/20170801015229_change_type_for_column_status_for_invoices.rb:1:in `<top (required)>' 
bin/rails:4:in `require' 
bin/rails:4:in `<main>' 
PG::DuplicateColumn: ERROR: column "tutor_pay" of relation "invoices" already exists 
/Users/jameshong/Projects/tad_is_my_boss/toptutoring/db/migrate/20170801015229_change_type_for_column_status_for_invoices.rb:2:in `<class:ChangeTypeForColumnStatusForInvoices>' 
/Users/jameshong/Projects/tad_is_my_boss/toptutoring/db/migrate/20170801015229_change_type_for_column_status_for_invoices.rb:1:in `<top (required)>' 
bin/rails:4:in `require' 
bin/rails:4:in `<main>' 
Tasks: TOP => db:rollback 
(See full trace by running task with --trace) 

正如您所看到的,遷移正確地將列tutor_pay添加到關係發票。但是,當我回滾時,執行的SQL是ADD COLUMN而不是DROP COLUMN。根據導軌指南,add_column應該是可逆的遷移。我試過用改變表來代替。我仍然遇到同樣的問題。

非常感謝,如果有人能幫我弄明白這一點。

回答

3

使用下面的遷移:

class ChangeTypeForColumnStatusForInvoices < ActiveRecord::Migration[5.1] 
    def change 
    add_column :invoices, :tutor_pay, :integer 
     remove_column :invoices, :status, :integer 
     add_column :invoices, :status, :string 
    end 
    end 
end 

或者你也可以用於執行遷移& self.down塊回滾for more info

+0

是的,我剛纔知道了。感謝您儘快回答! –

+1

@james如果有用的話,你可以註冊並接受這個答案。 –

+0

剛剛做到了!之前它不會讓我,因爲我也發佈了答案。感謝您花時間在這裏回答問題! –

0

我可能需要一些睡眠創建self.up塊。

沒有意識到我不得不將所有這些都包含在變更方法中。

class ChangeTypeForColumnStatusForInvoices < ActiveRecord::Migration[5.1] 
    def change  
    add_column :invoices, :tutor_pay, :integer 
    remove_column :invoices, :status, :integer 
    add_column :invoices, :status, :string 
    end 
end