Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-5683

dependent: :destroy destroys the related object

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 7.5.4
    • Component/s: Docs
    • Labels:
      None

      class TimeTable
        include Mongoid::Document
      
        belongs_to :account, class_name: 'Account', inverse_of: :time_tables
        belongs_to :company_account, class_name: 'Account', inverse_of: :company_time_table, optional: true
      end
      
      class Account
        include Mongoid::Document
      
        has_one :company_time_table, class_name: 'TimeTable', inverse_of: :company_account, dependent: :destroy, validate: false
        has_many :time_tables, class_name: 'TimeTable', inverse_of: :account, dependent: :destroy, validate: false
      end
      
      account = Account.create!
      ex_time_table = TimeTable.create(account:, company_account: account)
      new_time_table = TimeTable.create(account:)
      
      account.company_time_table = new_time_table # this codes produce the deletion of the ex_time_table document
      

      The dependent: :destroy from the company_time_table relation, produces the deletion of the object when is unassigned as company_time_table from the account.

      This is completely unexpected, the dependent should only be affected when the account is destroyed.

            Assignee:
            alex.bevilacqua@mongodb.com Alex Bevilacqua
            Reporter:
            alex.bevilacqua@mongodb.com Alex Bevilacqua
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: