如何在 Rails 2 上级联删除

发布于 2024-12-07 18:35:01 字数 673 浏览 2 评论 0原文

数据库设计:

contracts
  • id
  • name
allotment
  • id
  • namecontract_id
  • 我有这样的
allotments_rooms
  • id
  • allotment_id
  • room_id
  • 1contract has 1 allotment
  • 1 allotment has N allotments_rooms

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

所以,当我删除合同时,Allotment被完美删除,但allotments_rooms却没有。

为什么?

谢谢你!

I have this DB design:

contracts

  • id
  • name

allotment

  • id
  • name
  • contract_id

allotments_rooms

  • id
  • allotment_id
  • room_id
  • 1 contract has 1 allotment
  • 1 allotment has N allotments_rooms

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.

Why?

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心的位置 2024-12-14 18:35:01

当您使用 :dependent =>; :delete_all,您销毁依赖记录没有调用他们的destroy方法,这样他们就没有机会销毁自己的关联记录。尝试 :dependent => :delete 代替。

如果你想在数据库级别设置 FK 约束,我还建议使用 foreigner gem (尽管我不这样做)不知道它是否适用于 Rails 2)

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. Try :dependent => :delete instead.

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文