我正在运行ActiveRecord 3.0.0版。我知道你可以用外键创建列,比如
create_table "my_things" do |t| t.reference "other_thing_id" end
但我忘记了,只是把它变成了一个普通的整数。现在,我添加了一个类似
execute("alter table my_things add constraint fk_other_thing foreign key (other_thing_id) references other_things(id)")
这很好,但在schema.rb(我希望 t.reference ,所以如果我加载了一个模式,就不会得到约束。解决这个问题的最好方法是什么?
t.reference
你可能对 foreigner . 它帮助您在迁移中创建外键,并将它们添加到schema.rb中。