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 应该是一个可逆的迁移。我试过改用更改表。我仍然遇到同样的问题。

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

4

2 回答 2

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用于执行迁移self.down的块和用于回滚的块以获取更多信息

于 2017-08-02T07:09:36.343 回答
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
于 2017-08-02T07:14:10.777 回答