Rails 5:db:migrate 全球化失败 gem PG::UndefinedTable

Rails 5: db:migrate fails with globalize gem PG::UndefinedTable

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

我目前使用的gems:

在我的 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,这不是从 class 名称推断出来的:/

如果你的activerecord版本是5.1.4,试试这个:

class CreateQuotas < ActiveRecord::Migration[5.1]

想了很久,终于明白问题所在了。

Rails 4 允许将单数化 Quota 模型复数化为 quotas。 Rails 5 单数化 Quotum 复数化为 quota.

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

重命名所有模型和迁移(QuotaQuotumquotasquota)修复了 Rails 5.1 中的这个问题。