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

Bad conflicts key for atomic_updates when deeply nested structure

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 7.1.1
    • Component/s: Persistence
    • None

      Example:

      class A
                include Mongoid::Document
                include Mongoid::Timestamps::Short
      
                embeds_many :bs, cascade_callbacks: true
      
                accepts_nested_attributes_for :bs 
      
                field :name
              end
      
              class B
                include Mongoid::Document
                include Mongoid::Timestamps::Short
      
                embeds_many :cs, cascade_callbacks: true
      
                accepts_nested_attributes_for :cs 
      
                field :name
              end
      
              class C
                include Mongoid::Document
                include Mongoid::Timestamps::Short
      
                field :name
              end
      
              it 'push multiple' do
                a = A.create(name: "A Name")
                b = a.bs.create(name: "B Name")
      
                a.bs_attributes = { 
                  "0" => {
                    "cs_attributes" => {
                      "0" => {
                        "name" => "C Name"
                      }
                    },
                    "id" => b.id.to_s
                  },
                  "1" => {
                    "name" => "New B Name"
                  }
                }
      
                expect(a.atomic_updates).to eq({
                  "$push" => {"bs.0.cs"=>{"$each"=>[{"_id"=>b.cs.first.id, "name"=>"C Name"}]}},
                  :conflicts => {"$push"=>{"bs"=>{"$each"=>[{"_id"=>a.bs.last.id, "name"=>"New B Name"}]}}}
                })
      
                a.send(:prepare_update, {}) do
                   expect(a.atomic_updates).to eq({})
                end
      

      The first expectation looks good. bs.0.cs change in one push and other bs changes into other push.

      After update callbacks, it looks different: updated_at fields move to first push, but bs.0.cs moves to conflicts too. This calls Updating the path 'bs' would create a conflict at 'bs' (40) (on mongo:27017, legacy retry, attempt 1) error.

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            gsevka@gmail.com Всеволод Аврамов
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: