我在通过 Rails 生成器创建数据库表时遇到问题。
我personal_data命名了一个表并且工作正常。
class CreatePersonalData < ActiveRecord::Migration
def change
create_table :personal_data do |t|
t.string :name
t.string :lastName
t.string :dni
t.string :contact
t.references :user, index: true, foreign_key: true
t.timestamps null: false
end
end
end
但我创建了另一个表,其中引用了个人数据
class CreateMerchants < ActiveRecord::Migration
def change
create_table :merchants do |t|
t.string :storeName
t.references :personal_data, index: true, foreign_key: true
t.references :category, index: true, foreign_key: true
t.string :webPage
t.string :city
t.timestamps null: false
end
end
end
当 a 运行迁移而不是寻找personal_data_id迁移时,迁移会查找personal_datum_id并抛出和异常。
PG::UndefinedColumn: ERROR: no existe la columna «personal_datum_id» referida en la llave foránea :更改表"商家"添加约束"fk_rails_e4239c30dc" 外键("personal_datum_id"( 参考文献 "personal_data" ("id"(
我将错误翻译成英文
外键约束中引用的列"personal_datum_id"不存在
我不知道为什么它很混乱,但也许你可以设置你的屈折,使personal_data的复数是personal_data?
在文件中:config/initializers/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
inflect.irregular 'personal_data', 'personal_data'
end
personal_data.rb 模型中添加self.table_name = 'personal_data'
并运行迁移