我正在使用Rails 5和PostGres 9.4。我的表中有两列将引用另一个表。另一个表称为“encrypted_fields”。我想将我的列命名为“enc_field1_encrypted_field_id”和“enc_field2_encrypted_field_id”。我尝试了这次迁移
class MySecureInfo < ActiveRecord::Migration[5.0]
def change
create_table :secure_accounts do |t|
t.references :encrypted_field, :enc_field1_encrypted_field_id, index: true, foreign_key: true
t.references :encrypted_field, :enc_field2_encrypted_field_id, index: true, foreign_key: true
end
end
end
但它会因以下错误而爆炸
PG::UndefinedTable: ERROR: relation "enc_field1s" does not exist
如何调整迁移以避免错误?
答案 0 :(得分:0)
这应该适用于Rails 5以后:
class MySecureInfo < ActiveRecord::Migration[5.0]
def change
create_table :secure_accounts do |t|
t.references :enc_field1_encrypted_field, index: true, foreign_key: { to_table: :encrypted_fields }
t.references :enc_field2_encrypted_field, index: true, foreign_key: { to_table: :encrypted_fields }
end
end
end