代码之家  ›  专栏  ›  技术社区  ›  user24359

ActiveRecord迁移中的外键与schema.rb

  •  2
  • user24359  · 技术社区  · 16 年前

    我正在运行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 ,所以如果我加载了一个模式,就不会得到约束。解决这个问题的最好方法是什么?

    1 回复  |  直到 16 年前
        1
  •  2
  •   Tomas Markauskas    16 年前

    你可能对 foreigner . 它帮助您在迁移中创建外键,并将它们添加到schema.rb中。

    推荐文章