简体   繁体   English

Rails应用程序中的参照完整性

[英]Referential integrity in Rails applications

I am new to rails, having come from Symfony (which was inspired by Rails). 我是Rails的新手,来自Symfony(受Rails启发)。

I am used to defining my database schema in full, and make sure my tables are in 3NF, if necessary 4NF etc. 我习惯于完整定义数据库架构,并确保我的表位于3NF中,如有必要,请确保为4NF等。

I couldn't find a way to explicitly specify the CONSTRAINTS at the database level, and after searching online, I came across several posts such as this one: Hacked Ref Integrity , which left me simply astonished. 我找不到在数据库级别明确指定约束的方法,在网上搜索后,我遇到了几篇这样的文章: Hacked Ref Integrity ,这让我非常惊讶。

Admittedly, the article I linked to is quite old, and hopefully, things would have improved by now. 诚然,我链接到的文章已经很老了,希望现在情况会有所改善。

The (apparent?) lack of RI in rails makes the whole framework seem quite amateurish by comparison to Symfony, which it inspired. 与Rails相比,(显然?)Rails中缺少RI使得整个框架显得比较业余。

Please say it ain't so ... 请说不是...

The initial approach of rails was to put responsibility for this in the application domain. Rails的最初方法是对此负责在应用程序域中。 This was a conscious compromise, and you will find a lot of discussion about it, if you look for it 这是一个有意识的妥协,如果您寻找它,将会发现很多关于它的讨论。

However since more and more developers shared your concerns, there have indeed evolved several ways to recognize the databases role in this. 但是,由于越来越多的开发人员分享了您的担忧,因此确实已经发展出几种方法来识别数据库中的角色。

As the article you mentioned also shows , you can actually explicitly specify a lot of the constraints in the migrations with the normal migrations: 正如您提到的文章还显示的那样 ,实际上,您可以在常规迁移中显式指定迁移中的许多约束:

http://guides.rubyonrails.org/active_record_migrations.html#foreign-keys http://guides.rubyonrails.org/active_record_migrations.html#foreign-keys

add_foreign_key :articles, :authors

Or you can go one step further and use extensions like the schmea plus gem 或者,您可以更进一步,使用schmea plus gem等扩展程序

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

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