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

Uniqueness validation runs twice, including after the document is inserted

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

      I'm consistently seeing entries like this in our logs when our model classes contain a uniqueness validation. In this case, the email field must be unique:

      COMMAND database=my_db_production command={:count=>:users, :query=>
      {"email"=>/\Aredacted@gmail\.com$/i, "account_id"=>"123abc"}}
      
      INSERT database=my_db_production collection=users documents=[{"_id"=>"5018223a987b47700b000001", 
      "email"=>"redacted@gmail.com", "created"=>1970-01-16 13:15:47 UTC, "account_id"=>"123abc", 
      "registered"=>1970-01-16 13:15:47 UTC, "updated_at"=>2012-07-31 15:04:21 UTC, 
      "created_at"=>2012-07-31 15:04:21 UTC, "influence"=>:unknown, "session_count"=>0, "revenue"=>0, 
      "referred_users_count"=>0}] flags=[]
      
      COMMAND database=my_db_production command={:count=>:users, :query=>
      {"email"=>/\Aredacted@gmail\.com$/i, "_id"=>{"$ne"=>"5018223a987b47700b000001"}, 
      "account_id"=>"123abc"}}
      

      It appears that the validation callbacks (or this one, at a minimum) are firing after the insert operation, in addition to the proper pre-insert / update firing. This is especially problematic because the additional {{"_id"=>

      {"$ne"=>"5018223a987b47700b000001"}

      }} criterion seems to cause the query to miss the index, as this query is very slow, where the first one is fast (using Mongo 2.0.4, fwiw). It seems as though the underlying problem is that the validation callbacks are fired after the document is persisted, though.

            Assignee:
            Unassigned Unassigned
            Reporter:
            jeffwatts jeffwatts
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: