我正在尝试将 Rails 应用程序从 4.2 升级到 5.1。
我目前使用的宝石:
- 宝石'活动记录,'5.1.4
- 宝石'全球化','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
,这不是从类名中推断出来的:/