在Rails 5.1迁移中添加列不能正确回滚

时间:2017-08-02 07:03:40

标签: ruby-on-rails migration rollback ruby-on-rails-5.1

这是我的第一个问题所以请善待。 我最近碰到了这个问题。 添加列的迁移无法正常回滚。 我总是可以用SQL语句做到这一点,但我真的想弄清楚为什么这不起作用。

我的迁移代码如下所示。

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。根据rails指南,add_column应该是可逆的迁移。我尝试过使用更改表。我仍然遇到同样的问题。

如果有人能帮我解决这个问题,我将不胜感激。

2 个答案:

答案 0 :(得分: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.up块来执行迁移&amp; self.down阻止回滚for more info

答案 1 :(得分:0)

我可能需要一些睡眠。

没有意识到我必须在改变方法中包含所有这些。

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