简体   繁体   English

为什么有人会使用宝石外国人?

[英]Why would someone use the gem foreigner?

This is most likely a noob question since people use this gem and a lot of people love it, but I don't get the purpose. 这很可能是一个菜鸟问题,因为人们使用这个宝石并且很多人喜欢它,但我没有达到目的。 I'm looking at a project and its been used here many times in places such as t.references :foreign_key_table_name , :foreign_key => true , add_foreign_key :table :foreign_key_table_name, :options , and in a create t.foreign_key :foreign_key_table_name . 我正在查看一个项目,它在t.references :foreign_key_table_name , :foreign_key => trueadd_foreign_key :table :foreign_key_table_name, :options以及create t.foreign_key :foreign_key_table_name Hope those weren't confusing since they're out of context. 希望这些不会让人感到困惑,因为它们已脱离背景。

But I don't get how this is different from what rails does built in with t.references :foreign_key_table_name or from me just adding t.integer :foreign_key_table_name_id ? 但是我不知道这与rails使用t.references :foreign_key_table_name建立的t.references :foreign_key_table_name或者只是添加t.integer :foreign_key_table_name_id does it simply make it more readable by making clear that this is a 'foreign key'? 通过明确这是一个“外键”,它是否只是让它更具可读性? I could just add a comment instead of a gem if thats the case... The only advantage I see is that you can move options such as :dependent into the migration instead of having it in the model, but who cares? 如果是这样的话我可以添加注释而不是gem ...我看到的唯一优势是你可以移动选项,例如:dependent于迁移而不是在模型中使用它,但是谁在乎呢?

Some database engines support legit foreign key constraints: if someone tries to save a Child with a parent_id of 5, but there's no Parent with id 5, then the database itself (not Rails) will reject the record if there's a foreign key constraint linking children.parent_id and parents.id . 一些数据库引擎支持合法的外键约束:如果有人试图保存parent_id为5的Child ,但是没有Parent id 5,那么如果存在链接children.parent_id项的外键约束,则数据库本身(不是Rails)将拒绝该记录children.parent_idparents.id

A foreign key can also specify what happens if the parent is deleted: in MySQL, for example, we can delete or nullify the dependent records, like how Rails does with :dependent , or even just straight-up reject the deletion and throw an error instead. 外键还可以指定删除父项时会发生什么:例如,在MySQL中,我们可以删除或取消依赖记录,例如Rails如何处理:dependent ,或者甚至直接拒绝删除并抛出错误代替。

Since not all database engines offer this functionality, Rails offers to emulate it with :dependent , and it's nice to have it on the software level so that dependent child records can fire their destroy callbacks when the parent is deleted. 由于并非所有数据库引擎都提供此功能,因此Rails提供模拟它:dependent ,并且很高兴在软件级别上使用它,以便依赖子记录可以在删除父级时触发其destroy回调。 Since the feature is engine-independent and therefore pretty much schema-independent, Rails doesn't handle the creation/deletion of foreign keys. 由于该功能与引擎无关,因此几乎与模式无关,因此Rails不处理外键的创建/删除。 That's where foreigner comes in: if your engine supports foreign key constraints, and you want that extra confident in your data integrity, foreigner can help with that. 这就是foreigner进来的地方:如果您的引擎支持外键约束,并且您希望对数据完整性有额外的信心,那么foreigner可以提供帮助。

Resurrecting an old question here, but… 在这里复活一个老问题,但......

Having rails enforce the relationship is fine, within rails itself. 拥有rails强制执行这种关系很好,在rails本身。

However, if your project grows to have code that also accesses these tables from other languages, that will not have the benefit of rails enforcing the relations. 但是,如果您的项目增长到具有也可以从其他语言访问这些表的代码,那么这将不会有rails强制执行关系的好处。 These foreign key constraints are baked into the SQL tables themselves, so can protect non-rails code. 这些外键约束被绑定到SQL表本身,因此可以保护非rails代码。

This will also protect you if you need to perform datafixes or otherwise manipulate your data via native SQL. 如果您需要执行数据修正或以其他方式通过本机SQL操作数据,这也将保护您。

Another reason is that some documentation tools for SQL look at foreign keys on the DB, so it is cool to have a gem that generates them. 另一个原因是SQL的一些文档工具会查看数据库上的外键,因此拥有生成它们的gem很酷。 Rails 4 added the ability to define foreign keys in the same migration that creates the table with: Rails 4添加了在创建表的同一迁移中定义外键的功能:

t.references :something, foreign_key: true

And the generators will do this for you if you use the references type. 如果您使用references类型,生成器将为您执行此操作。 Rails adds an index on something_id by default when using foreign_key like this 当使用这样的foreign_key时,Rails默认在something_id上添加索引

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM