简体   繁体   English

RoR关联:has_one + has_many depedent::destroy

[英]RoR associations: has_one + has_many depedent: :destroy

I don't get why I'm having orphans records when I try to destroy a user. 当我尝试销毁用户时,我不明白为什么会有孤儿记录。 A User has one Cart which has many CartItem 用户 具有其中有许多 CartItem 一个


User has one cart: 用户有一个购物车:

 class User < ApplicationRecord has_one :cart, dependent: :destroy has_many :cart_items, through: :cart, dependent: :destroy has_many :samples, through: :cart_items, source: :cartable, source_type: 'Sample' has_many :tracks, through: :cart_items, source: :cartable, source_type: 'Track' end 

:dependent :依赖

Controls what happens to the associated object when its owner is destroyed: 控制当关联对象的所有者被销毁时会发生什么情况:

  • :destroy causes the associated object to also be destroyed :destroy导致关联的对象也被破坏

https://apidock.com/rails/v5.2.3/ActiveRecord/Associations/ClassMethods/has_one https://apidock.com/rails/v5.2.3/ActiveRecord/Associations/ClassMethods/has_one

Cart has many items: 购物车有很多物品:

 class Cart < ApplicationRecord belongs_to :user has_many :cart_items, dependent: :destroy has_many :samples, through: :cart_items, source: :cartable, source_type: 'Sample' has_many :tracks, through: :cart_items, source: :cartable, source_type: 'Track' end 

:dependent :依赖

Controls what happens to the associated objects when their owner is destroyed. 控制销毁所有者时关联对象发生的情况。 Note that these are implemented as callbacks, and Rails executes callbacks in order. 注意,这些被实现为回调,并且Rails按顺序执行回调。 Therefore, other similar callbacks may affect the :dependent behavior, and the :dependent behavior may affect other callbacks. 因此,其他类似的回调可能会影响:depend行为,而:dependent行为可能会影响其他回调。

  • :destroy causes all the associated objects to also be destroyed. :destroy导致所有关联的对象也被破坏。

https://apidock.com/rails/v5.2.3/ActiveRecord/Associations/ClassMethods/has_many https://apidock.com/rails/v5.2.3/ActiveRecord/Associations/ClassMethods/has_many

And items: 和项目:

 class CartItem < ApplicationRecord belongs_to :cart belongs_to :cartable, polymorphic: true end 


I'd like to be able to destroy a User with for example User.last.destroy , but instead I've an error: 我希望能够使用User.last.destroy销毁一个User,但是却出现了一个错误:

 ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: update or delete on table "users" violates foreign key constraint "fk_rails_ea59a35211" on table "carts" DETAIL: Key (id)=(227) is still referenced from table "carts". 

I was thinking that has_one :cart, dependent: :destroy would do the job but it looks like I'm wrong. 我当时以为has_one :cart, dependent: :destroy可以完成这项工作,但看起来我错了。 What am I missing ? 我想念什么?

Thanks for your time 谢谢你的时间

I was facing this problem on my development machine, then I found root cause of this issue after lot of debugging and analysis. 我在开发机器上遇到了这个问题,然后经过大量的调试和分析,我发现了这个问题的根本原因。 Postgres was creating extra constraints which was causing this. Postgres造成了额外的限制,从而导致了这种情况。 You need to drop the constraints. 您需要删除约束。 You can do this by a migration. 您可以通过迁移来做到这一点。

rails g migration remove_fk_constraints

class RemoveFkConstrains < ActiveRecord::Migration[5.2]
  def up
    execute "ALTER TABLE carts DROP CONSTRAINT fk_rails_ea59a35211;"
  end
end

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

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