简体   繁体   English

轨道中的参照完整性

[英]referential integrity in rails

So, I just came across the fact that rails does not support referential integrity regarding foreign keys and was fairly surprised. 所以,我刚刚发现rails不支持关于外键的引用完整性,并且相当惊讶。 So, what's the best way to manage this? 那么,管理这个的最佳方法是什么? Is there a "rails" way for dealing with referential integrity? 是否存在处理参照完整性的“轨道”方式?

Ideally the app should not have to deal with all this; 理想情况下,应用程序不应该处理所有这些; the db should. 数据库应该。 I was looking at plugins like foreigner. 我正在寻找像外国人一样的插件。 I wonder if this method has some shortcomings. 我想知道这种方法是否有一些缺点。 How is this normally dealt with in rails? 这通常如何处理轨道?

It's a design decision for Rails ActiveRecord. 这是Rails ActiveRecord的设计决定。

I consider stored procedures and constraints vile and reckless destroyers of coherence. 我认为存储过程和约束是卑鄙的和鲁莽的连贯性的驱逐者。 No, Mr. Database, you can not have my business logic. 不,数据库先生,你不能拥有我的业务逻辑。 Your procedural ambitions will bear no fruit and you'll have to pry that logic from my dead, cold object-oriented hands. 你的程序野心将没有结果,你必须从我死去的,冷酷的面向对象的手中撬开那个逻辑。

Choose a single layer of cleverness - DHH 选择一层聪明 - DHH

So the answer is that referential integrity handled by the DB is not the Rails way at all. 所以答案是DB处理的引用完整性根本不是Rails方式。

You can provide programmatic referential integrity by using 您可以使用提供程序化参照完整性

,:dependent => :restrict

on your has_one and has_many statements! 在你的has_one和has_many语句中!

Blog describing this and how to check before error occurs 博客描述此以及如何在发生错误之前进行检查

Relevant Active Record docs 相关的Active Record文档

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

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