2017-08-02 11 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はDROP COLUMNではなく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 

またはあなたはまた、私はおそらくいくつかの睡眠を必要とし、ロールバックfor more info

+0

いや私はちょうど今それを理解しました。あまりにも早く答えてくれてありがとう! –

+1

@james便利な場合は、この回答をupvote&acceptすることができます。 –

+0

ちょうどやった!私が答えを投稿したので、以前はそれは私を許さなかった。ここに答えを入れる時間をとってくれてありがとう! –

0

の移行& self.downブロックを実行するためのself.upブロックを作成することができます。

私はこれをすべて変更メソッドでラップしなければならないことに気が付きませんでした。

関連する問題