Skip to content

Commit 0b78eeb

Browse files
committed
edits.
1 parent 1ce32fe commit 0b78eeb

File tree

3 files changed

+5
-15
lines changed

3 files changed

+5
-15
lines changed

django_mongodb_backend/expressions/builtins.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
Value,
2626
When,
2727
)
28-
from django.db.models.indexes import IndexExpression
2928
from django.db.models.sql import Query
3029

3130
from django_mongodb_backend.query_utils import process_lhs
@@ -107,19 +106,6 @@ def expression_wrapper(self, compiler, connection):
107106
return self.expression.as_mql(compiler, connection, as_expr=True)
108107

109108

110-
def index_expression(self, compiler, connection, as_expr=False): # noqa: ARG001
111-
result = []
112-
for expr in self.get_source_expressions():
113-
if expr is None:
114-
continue
115-
for sub_expr in expr.get_source_expressions():
116-
try:
117-
result.append(sub_expr.as_mql(compiler, connection))
118-
except FullResultSet:
119-
result.append(Value(True).as_mql(compiler, connection))
120-
return result
121-
122-
123109
def negated_expression(self, compiler, connection):
124110
return {"$not": expression_wrapper(self, compiler, connection)}
125111

@@ -258,7 +244,6 @@ def register_expressions():
258244
Exists.as_mql_expr = exists
259245
ExpressionList.as_mql = process_lhs
260246
ExpressionWrapper.as_mql_expr = expression_wrapper
261-
IndexExpression.as_mql = index_expression
262247
NegatedExpression.as_mql_expr = negated_expression
263248
OrderBy.as_mql_expr = partialmethod(order_by, as_expr=True)
264249
OrderBy.as_mql_path = partialmethod(order_by, as_expr=False)

django_mongodb_backend/indexes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
from django.db import NotSupportedError
66
from django.db.models import FloatField, Index, IntegerField
77
from django.db.models.expressions import OrderBy
8+
from django.db.models.indexes import IndexExpression
89
from django.db.models.lookups import BuiltinLookup
910
from django.db.models.sql.query import Query
1011
from django.db.models.sql.where import AND, XOR, WhereNode
1112
from pymongo import ASCENDING, DESCENDING
1213
from pymongo.operations import IndexModel, SearchIndexModel
1314

1415
from django_mongodb_backend.fields import ArrayField
16+
from django_mongodb_backend.query_utils import process_lhs
1517

1618
from .query_utils import process_rhs
1719

@@ -342,4 +344,5 @@ def register_indexes():
342344
BuiltinLookup.as_mql_idx = builtin_lookup_idx
343345
Index._get_condition_mql = _get_condition_mql
344346
Index.get_pymongo_index_model = get_pymongo_index_model
347+
IndexExpression.as_mql = process_lhs
345348
WhereNode.as_mql_idx = where_node_idx

django_mongodb_backend/schema.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ def _remove_field_index(self, model, field, column_prefix=""):
353353
collection.drop_index(index_names[0])
354354

355355
def _check_expression_indexes_applicable(self, expressions):
356+
# Check if all expressions are field references or ORDER BY expressions on a field.
356357
return all(
357358
isinstance(expression.expression if isinstance(expression, OrderBy) else expression, F)
358359
for expression in expressions
@@ -373,6 +374,7 @@ def _unique_supported(
373374
and (
374375
not expressions
375376
or self.connection.features.supports_expression_indexes
377+
# Expression indexes are partially supported.
376378
or self._check_expression_indexes_applicable(expressions)
377379
)
378380
and (

0 commit comments

Comments
 (0)