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

3.0.0rc duplicate inserts to a unique index only errors every alternate time (even in safe mode)

    • Type: Icon: Task Task
    • Resolution: Done
    • 3.0.0
    • Affects Version/s: None
    • Component/s: None
    • None

      It the 3.0.0rc release, trying to create two records with the same unique index, doesn't fail every time (even with safe: true).

      class MyClass
        include Mongoid::Document
      
        field :my_field, type: String
      
        index({ my_field: 1 }, {unique: true, name: 'my_field'})
      end
      
      MyClass.create_indexes
      
      MyClass.create!(my_field: 'foo').with(safe: true)
      # => (correctly) returns the document
      MyClass.create!(my_field: 'foo').with(safe: true)
      # => (correctly) raises Moped::Errors::OperationFailure because of MongoDB E11000 duplicate key error
      MyClass.create!(my_field: 'foo').with(safe: true)
      # => returns a document! :/
      MyClass.create!(my_field: 'foo').with(safe: true)
      # => (correctly) raises Moped::Errors::OperationFailure because of MongoDB E11000 duplicate key error
      

      Note that only record actually reaches the database though.

      MyClass.count
      # => 1
      

            Assignee:
            durran Durran Jordan
            Reporter:
            RicSwirrl RicSwirrl [X]
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: