Skip to content

Commit b84aae2

Browse files
committed
Raise NotSupportedError on QuerySet extras
- RawSQL - ExtraWhere
1 parent 8b98b58 commit b84aae2

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

django_mongodb/expressions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
F,
1414
NegatedExpression,
1515
OrderBy,
16+
RawSQL,
1617
Ref,
1718
ResolvedOuterRef,
1819
Star,
@@ -21,6 +22,7 @@
2122
When,
2223
)
2324
from django.db.models.sql import Query
25+
from django.db.models.sql.where import ExtraWhere
2426

2527

2628
def case(self, compiler, connection):
@@ -66,6 +68,10 @@ def expression_wrapper(self, compiler, connection):
6668
return self.expression.as_mql(compiler, connection)
6769

6870

71+
def extra_where(self, compiler, connection): # noqa: ARG001
72+
raise NotSupportedError("QuerySet extras are not supported on MongoDB.")
73+
74+
6975
def f(self, compiler, connection): # noqa: ARG001
7076
return f"${self.name}"
7177

@@ -81,6 +87,8 @@ def order_by(self, compiler, connection):
8187
def query(self, compiler, connection): # noqa: ARG001
8288
raise NotSupportedError("Using a QuerySet in annotate() is not supported on MongoDB.")
8389

90+
def raw_sql(self, compiler, connection): # noqa: ARG001
91+
raise NotSupportedError("QuerySet extras are not supported on MongoDB.")
8492

8593
def ref(self, compiler, connection): # noqa: ARG001
8694
prefix = (
@@ -125,11 +133,13 @@ def register_expressions():
125133
Case.as_mql = case
126134
Col.as_mql = col
127135
CombinedExpression.as_mql = combined_expression
136+
ExtraWhere.as_mql = extra_where
128137
ExpressionWrapper.as_mql = expression_wrapper
129138
F.as_mql = f
130139
NegatedExpression.as_mql = negated_expression
131140
OrderBy.as_mql = order_by
132141
Query.as_mql = query
142+
RawSQL.as_mql = raw_sql
133143
Ref.as_mql = ref
134144
ResolvedOuterRef.as_mql = ResolvedOuterRef.as_sql
135145
Star.as_mql = star

django_mongodb/features.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
6868
"aggregation.tests.AggregateTestCase.test_reverse_fkey_annotate",
6969
"aggregation_regress.tests.AggregationTests.test_annotation_disjunction",
7070
"aggregation_regress.tests.AggregationTests.test_decimal_aggregate_annotation_filter",
71-
# QuerySet.extra(select=...) should raise NotSupportedError instead of:
72-
# 'RawSQL' object has no attribute 'as_mql'.
73-
"aggregation_regress.tests.AggregationTests.test_annotate_with_extra",
74-
"aggregation_regress.tests.AggregationTests.test_annotation",
75-
"aggregation_regress.tests.AggregationTests.test_more_more3",
76-
"aggregation_regress.tests.AggregationTests.test_more_more_more3",
77-
# QuerySet.extra(where=...) should raise NotSupportedError instead of:
78-
# 'ExtraWhere' object has no attribute 'as_mql'.
79-
"many_to_one.tests.ManyToOneTests.test_selects",
8071
# Incorrect JOIN with GenericRelation gives incorrect results.
8172
"aggregation_regress.tests.AggregationTests.test_aggregation_with_generic_reverse_relation",
8273
"generic_relations.tests.GenericRelationsTests.test_queries_content_type_restriction",
@@ -673,6 +664,17 @@ def django_test_expected_failures(self):
673664
"backends.base.test_base.DatabaseWrapperLoggingTests",
674665
"migrations.test_operations.OperationTests.test_run_python_atomic",
675666
},
667+
"QuerySet extras are not supported on MongoDB": {
668+
# QuerySet.extra(select=...) raises NotSupportedError instead of:
669+
# 'RawSQL' object has no attribute 'as_mql'.
670+
"aggregation_regress.tests.AggregationTests.test_annotate_with_extra",
671+
"aggregation_regress.tests.AggregationTests.test_annotation",
672+
"aggregation_regress.tests.AggregationTests.test_more_more3",
673+
"aggregation_regress.tests.AggregationTests.test_more_more_more3",
674+
# QuerySet.extra(where=...) should raise NotSupportedError instead of:
675+
# 'ExtraWhere' object has no attribute 'as_mql'.
676+
"many_to_one.tests.ManyToOneTests.test_selects",
677+
},
676678
}
677679

678680
@cached_property

0 commit comments

Comments
 (0)