mongoengine
mongoengine copied to clipboard
unique=True doesn't after drop_collection
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 :-('
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.