gpt4 book ai didi

ruby - activerecord 迁移中的外键与 schema.rb

转载 作者:太空宇宙 更新时间:2023-11-03 16:11:14 24 4
gpt4 key购买 nike

我正在运行 activerecord 3.0.0beta。我知道您可以使用外键创建列,例如

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),所以如果我执行模式加载,我不会得到我的约束。解决该问题的最佳方法是什么?

最佳答案

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

关于ruby - activerecord 迁移中的外键与 schema.rb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3105631/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com