From 8c632a2bd8429a7a09b2125c5c2aae07730ded13 Mon Sep 17 00:00:00 2001 From: Atifa Perveen Date: Wed, 10 Oct 2018 16:09:01 +0500 Subject: [PATCH 1/2] Refs #81768: Added checks to prevent creating additional indexes on _id field. Sparse, unique and background options cannot be provided with index on primary key. Mongo 3.4 has strict validations on index creations and throws exceptions instead of just warnings. --- django_mongodb_engine/creation.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/django_mongodb_engine/creation.py b/django_mongodb_engine/creation.py index bd0667f9..b22f6566 100644 --- a/django_mongodb_engine/creation.py +++ b/django_mongodb_engine/creation.py @@ -70,7 +70,8 @@ def _handle_newstyle_indexes(self, ensure_index, meta, indexes): # field doesn't need an index. continue column = '_id' if field.primary_key else field.column - ensure_index(column, unique=field.unique) + if not column == '_id': + ensure_index(column, unique=field.unique) # Django unique_together indexes. indexes = list(indexes) @@ -122,9 +123,10 @@ def _handle_oldstyle_indexes(self, ensure_index, meta): # field doesn't need an index. continue column = '_id' if field.primary_key else field.column - if field.name in descending_indexes: - column = [(column, DESCENDING)] - ensure_index(column, unique=field.unique, + if not column == '_id': + if field.name in descending_indexes: + column = [(column, DESCENDING)] + ensure_index(column, unique=field.unique, sparse=field.name in sparse_indexes) def create_compound_indexes(indexes, **kwargs): From 0cf71e13ba2b9b92c04fd0e47d8ce24679d3d5c6 Mon Sep 17 00:00:00 2001 From: Arsalan Khairani Date: Fri, 2 Nov 2018 13:25:32 +0500 Subject: [PATCH 2/2] Indentation Fix --- django_mongodb_engine/creation.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/django_mongodb_engine/creation.py b/django_mongodb_engine/creation.py index b22f6566..fb47f0b3 100644 --- a/django_mongodb_engine/creation.py +++ b/django_mongodb_engine/creation.py @@ -126,8 +126,7 @@ def _handle_oldstyle_indexes(self, ensure_index, meta): if not column == '_id': if field.name in descending_indexes: column = [(column, DESCENDING)] - ensure_index(column, unique=field.unique, - sparse=field.name in sparse_indexes) + ensure_index(column, unique=field.unique, sparse=field.name in sparse_indexes) def create_compound_indexes(indexes, **kwargs): # indexes: (field1, field2, ...).