mongoengine icon indicating copy to clipboard operation
mongoengine copied to clipboard

unique=True doesn't after drop_collection

Open sirkonst opened this issue 14 years ago • 1 comments

Test code:


##### Init

class Account(Document):    
   name = StringField(unique=True)

connect(...)


##### Focus:

Account.drop_collection()

acc_1 = Account.objects.create(name='test')

try:
   acc_1_dup = Account.objects.create(name='test')
except OperationError:
   print 'Good'

# Now drop again
Account.drop_collection()

acc_2 = Account.objects.create(name='test')

try:
   acc_2_dup = Account.objects.create(name='test')
except OperationError:
   print 'Good'
else:
   print 'Bad :-('

sirkonst avatar Oct 27 '11 10:10 sirkonst

This is particularly annoying when you're writing unit tests! I was quite confused by this. It also happens when you do it with pymongo directly.

However, I now remove the collections instead of dropping the database, which doesn't have this problem and also is a lot faster.

gitaarik avatar Sep 05 '13 15:09 gitaarik