简体   繁体   English

如何在Rails 2上的级联上删除

[英]How to delete on cascade on Rails 2

I have this DB design: 我有这个数据库设计:

contracts 合同
  • id ID
  • name 名称
allotment 配股
  • id ID
  • name 名称
  • contract_id contract_id
allotments_rooms allotments_rooms
  • id ID
  • allotment_id allotment_id
  • room_id ROOM_ID
  • 1 contract has 1 allotment 1份合同有1份配额
  • 1 allotment has N allotments_rooms 1个分配有N个分配_room

So… 所以…

 Contract has_one :allotment, :dependent => :delete_all Allotment belongs_to :contract Allotment has_many :allotments_rooms, :dependent => :delete_all Allotments_Rooms belongs_to :allotment 

So, when I delete a Contract, the Allotment is perfectly removed, but the allotments_rooms are not. 因此,当我删除合同时,分配完全被删除,但分配_room不是。

Why? 为什么?

Thank you! 谢谢!

when you use :dependent => :delete_all , you destroy dependent records without calling their destroy method , so they have no chance to destroy their own associated records. 当你使用:dependent => :delete_all ,你破坏依赖记录而不调用它们的destroy方法 ,所以他们没有机会销毁自己的相关记录。 Try :dependent => :delete instead. 尝试:dependent => :delete

i would also recommend using the foreigner gem if you want to set FK constraints at DB level (though i don't know if it works with rails 2) 如果你想在DB级别设置FK约束,我也建议使用外国人gem (虽然我不知道它是否适用于rails 2)

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

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