Skip to content

Commit f83a99f

Browse files
committed
Merge pull request #1 from Elastica/private-chaks-4708
Private chaks 4708
2 parents 48bb9fd + 1f8f7b7 commit f83a99f

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

django_mongodb_engine/creation.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
from pymongo import DESCENDING
44

5+
from pymongo.errors import OperationFailure
6+
7+
from random import randint
8+
59
from djangotoolbox.db.creation import NonrelDatabaseCreation
610

711
from .utils import make_index_list
@@ -39,7 +43,15 @@ def ensure_index(*args, **kwargs):
3943
print "Installing indices for %s.%s model." % \
4044
(meta.app_label, meta.object_name)
4145
ensure_index.first_index = False
42-
return collection.ensure_index(*args, **kwargs)
46+
try:
47+
return collection.ensure_index(*args, **kwargs)
48+
except OperationFailure, e:
49+
# Try with a short name for the index in case the
50+
# auto-generated index name is too long
51+
index_name = meta.object_name + "_index_" + str(randint(1,100000))
52+
print "Error installing index on %s: %s, trying shorter index name: %s" % (meta.object_name, str(e), index_name)
53+
return collection.ensure_index(*args, name=index_name, **kwargs)
54+
4355
ensure_index.first_index = True
4456

4557
newstyle_indexes = getattr(meta, 'indexes', None)

0 commit comments

Comments
 (0)