自动添加和删除引用完整性约束
是否有 Rails gem、插件或其他方法可以自动添加或删除数据库和模式中的所有 RI 约束?或者所有这些都以每张桌子为基础?类似于:
remove_all_foreign_key_constraints('comments')
Is there a Rails gem, plugin or other means to automatically add or remove ALL RI constraints from your DB and schema? Or ALL of them on a per table basis? Something like:
remove_all_foreign_key_constraints('comments')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道有任何宝石/插件。
但这很棘手,因为 Rails(和大多数适配器)假设引用完整性是通过应用程序的关联来维护的。
如果是这种情况(关联在模型中维护),那么您可以编写一个小脚本,根据模型的关联构建所需的 SQL 语句。
关联反射可能会有所帮助:
http://api.rubyonrails。 org/classes/ActiveRecord/Reflection/ClassMethods.html#method-i-reflect_on_all_associations
No gems/plugins I'm aware of.
This is tricky though because rails (and most adapters) assume the referential integrity is maintained through your application's association.
If that's the case (associations ARE maintained in models), then you could write a little script that would construct the required SQL statements based on the model's association.
Association reflection might help:
http://api.rubyonrails.org/classes/ActiveRecord/Reflection/ClassMethods.html#method-i-reflect_on_all_associations