Rails 5:db:migrate使用globalize gem PG :: UndefinedTable失败

时间:2017-12-22 17:48:50

标签: ruby-on-rails rails-migrations globalize

我正在尝试将Rails应用从4.2升级到5.1。

我目前正在使用的宝石:

  • gem' activerecord,' 5.1.4
  • gem' globalize',' 5.1.0.beta2'

在我的 Gemfile

gem 'globalize', git: 'https://github.com/globalize/globalize'
gem 'activemodel-serializers-xml'
gem 'globalize-accessors'
gem 'globalize3_helpers', git: 'https://github.com/mathieumahe/globalize3_helpers.git'

我有一个如下所示的迁移文件:

class CreateQuotas < ActiveRecord::Migration[5.0]
  def change
    create_table :quotas do |t|
      t.references :survey, index: true
      t.integer :target, default: 0
      t.timestamps null: false
    end

    reversible do |dir|
      dir.up { Quota.create_translation_table!(title: :string) }
      dir.down { Quota.drop_translation_table! }
    end
  end
end

并在 quota.rb 模型中设置了相应的translations指令:

class Quota < ApplicationRecord
  # ...
  translates :title
  # ...
end

运行迁移会导致以下错误:

rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedTable: ERROR:  relation "quota" does not exist
LINE 8:                WHERE a.attrelid = '"quota"'::regclass
                                          ^
:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,
                     c.collname, col_description(a.attrelid, a.attnum) AS comment
                FROM pg_attribute a
                LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
                LEFT JOIN pg_type t ON a.atttypid = t.oid
                LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation
               WHERE a.attrelid = '"quota"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum

我做错了什么或者我错过了一些明显的东西吗?

更新

我在此方面取得了一些进展。我已将SQL直接输入到迁移中,因此它看起来像这样:

class CreateQuotas < ActiveRecord::Migration[5.0]
  def change
    create_table :quotas do |t|
      t.references :survey, index: true
      t.integer :target, default: 0
      t.timestamps null: false
    end

    reversible do |dir|
      dir.up do
        execute <<-SQL
          SELECT a.attname, format_type(a.atttypid, a.atttypmod),
               pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,
               c.collname, col_description(a.attrelid, a.attnum) AS comment
          FROM pg_attribute a
          LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
          LEFT JOIN pg_type t ON a.atttypid = t.oid
          LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation
         WHERE a.attrelid = '"quotas"'::regclass
           AND a.attnum > 0 AND NOT a.attisdropped
         ORDER BY a.attnum
        SQL
      end
      # dir.up do
      #   Quota.create_translation_table! :title => :string
      # end
      #
      dir.down do
        Quota.drop_translation_table!
      end
    end
  end
end

这似乎有效。

基本上,Quota.create_translation_table! :title => :string指令正在使'"quota"'::regclass单一化导致失败。它传递'"quotas"'::regclass,这不是从类名推断出来的:/

2 个答案:

答案 0 :(得分:0)

如果您的activerecord版本是5.1.4,请尝试以下操作:

class CreateQuotas < ActiveRecord::Migration[5.1]

答案 1 :(得分:0)

经过多次头疼,我意识到问题所在。

Rails 4允许将单一化的Quota模型复数化为quotas。 Rails 5将Quotum单一化并将其复数为quota

这解释了'"quota"'::regclass错误,以及为什么强制它'"quotas"'::regclass阻止迁移失败。

重命名所有模型和迁移(QuotaQuotumquotasquota)在Rails 5.1中解决了这个问题。