Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoid unique index allowed duplicate values

I have a collection that has a username as a field. The Model defines this field to be unique. However I was able to insert a duplicate value in the database.

class Profile
  include Mongoid::Document
  include Mongoid::Paperclip

  field :username
  index({ username: 1 } , { unique: true })
end

The Collection however has 2 usernames that are the same

{ "_id" : ObjectId( "50b3b323421aa95da6000004" ),
  "username" : "marceloreuse" }

{ "_id" : ObjectId( "50b3b567421aa93d84000002" ),
  "username" : "marceloreuse" }

What went wrong here?

like image 383
Amit Avatar asked Nov 26 '12 18:11

Amit


1 Answers

I would double check your indexes - from the console try db.collection.getIndexes() and make sure your index is present.

In case you missed it, Mongoid doesn't auto build the index because you specified it - you need to run the included: rake db:mongoid:create_indexes.

like image 195
kmfk Avatar answered Nov 20 '22 11:11

kmfk