Skip to content

Commit 3c25544

Browse files
committed
temp hack to make unique indexes exclude nulls
1 parent 14b79a3 commit 3c25544

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

django_mongodb/schema.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,21 @@ def _get_name(field_names):
113113
def add_index(self, model, index, unique=False):
114114
if index.contains_expressions:
115115
return
116+
kwargs = {}
117+
if unique:
118+
kwargs = {
119+
"partialFilterExpression": {index.fields_orders[0][0]: {"$type": "string"}},
120+
"unique": True,
121+
}
122+
else:
123+
kwargs = {}
116124
idx = IndexModel(
117125
[
118126
(model._meta.get_field(field_name).column, 1 if order == "" else -1)
119127
for field_name, order in index.fields_orders
120128
],
121129
name=index.name,
122-
unique=unique,
130+
**kwargs,
123131
)
124132
self.connection.database[model._meta.db_table].create_indexes([idx])
125133

0 commit comments

Comments
 (0)