Ruby-on-rails 在 ActiveRecord 中创建外键约束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12428414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating foreign key constraints in ActiveRecord
提问by at.
How do I create foreign keys in ActiveRecord? I have something like the following in my models:
如何在 ActiveRecord 中创建外键?我的模型中有如下内容:
class Student < ActiveRecord::Base
attr_accessible :name, :level_id
belongs_to :level
end
class Level < ActiveRecord::Base
attr_accessible :number
has_many :students
end
But the schema.rb and development sqlite3 database don't have any indication foreign key constraints were setup with the level_id field. Is this something I have to do manually apart from ActiveRecord or Rails? Did I miss a step?
但是 schema.rb 和 development sqlite3 数据库没有任何指示外键约束是使用 level_id 字段设置的。除了 ActiveRecord 或 Rails 之外,这是我必须手动执行的操作吗?我错过了一步吗?
Using Rails 3.2.8
使用 Rails 3.2.8
采纳答案by mguymon
You do not need a foreign key constraints for ActiveRecord to correctly map the relationships. You can use validationsto have the Rails app ensure data integrity.
您不需要 ActiveRecord 的外键约束来正确映射关系。您可以使用验证让 Rails 应用程序确保数据完整性。
Rails migration do not provider helpers to create foreign keys. You can create your own SQL for the constraint in the migration or use the the Foreigner Gem. Foreigner will provide helper methods for creating constraints in a migration:
Rails 迁移不提供创建外键的助手。您可以为迁移中的约束创建自己的 SQL 或使用Foreigner Gem。Foreigner 将提供在迁移中创建约束的辅助方法:
add_foreign_key(:students, :levels)
回答by Segfault
If you have rails >= 4.2 and using the mysql, mysql2, or postgresqladapter, then you can use the add_foreign_keymethod in your migration like this:
如果你有轨> = 4.2和使用mysql,mysql2或postgresql适配器,那么您可以使用add_foreign_key您的迁移这样的方法:
# add a foreign key to `articles.author_id` referencing `authors.id`
add_foreign_key :articles, :authors
回答by DB140141
This is built into Rails now, you don't need Foreigner. See section 3.6 Foreign Keys here: http://edgeguides.rubyonrails.org/active_record_migrations.html
这现在内置于 Rails 中,您不需要 Foreigner。请参阅此处的第 3.6 节外键:http: //edgeguides.rubyonrails.org/active_record_migrations.html
回答by claptimes
Adding belong_toand has_manylines to your models makes Rails aware of their relationship and generates helper methods, but does not create FKs at the database level. To do that, you need to create and run a migration:
向模型中添加belong_to和has_many行使 Rails 意识到它们之间的关系并生成辅助方法,但不会在数据库级别创建 FK。为此,您需要创建并运行迁移:
rails g migration add_level_id_to_students level_id:integer
then rake db:migrate
然后耙分贝:迁移
If you want to generate a model with the foreign key part of it, you can use the referencesshortcut:
rails g model Student name:string level:references
如果要生成带有外键部分的模型,可以使用references快捷方式:
rails g model Student name:string level:references
Check out the Rails guidesfor more information!
查看Rails 指南以获取更多信息!
回答by xhenryx14
$ rails generate migration AddStudentRefToLevels student:references
which should make something like this
这应该是这样的
class AddStudentRefToLevels < ActiveRecord::Migration
def change
add_reference :levels, :student, index: true
end
end
taken from here http://guides.rubyonrails.org/migrations.html#creating-a-migration
取自这里http://guides.rubyonrails.org/migrations.html#creating-a-migration

