diff --git a/django_mongodb/expressions.py b/django_mongodb/expressions.py index d175a132a..7af3d71eb 100644 --- a/django_mongodb/expressions.py +++ b/django_mongodb/expressions.py @@ -13,6 +13,7 @@ F, NegatedExpression, OrderBy, + RawSQL, Ref, ResolvedOuterRef, Star, @@ -82,6 +83,10 @@ def query(self, compiler, connection): # noqa: ARG001 raise NotSupportedError("Using a QuerySet in annotate() is not supported on MongoDB.") +def raw_sql(self, compiler, connection): # noqa: ARG001 + raise NotSupportedError("RawSQL is not supported on MongoDB.") + + def ref(self, compiler, connection): # noqa: ARG001 prefix = ( f"{self.source.alias}." @@ -130,6 +135,7 @@ def register_expressions(): NegatedExpression.as_mql = negated_expression OrderBy.as_mql = order_by Query.as_mql = query + RawSQL.as_mql = raw_sql Ref.as_mql = ref ResolvedOuterRef.as_mql = ResolvedOuterRef.as_sql Star.as_mql = star diff --git a/django_mongodb/features.py b/django_mongodb/features.py index 72e8b2706..4667b2637 100644 --- a/django_mongodb/features.py +++ b/django_mongodb/features.py @@ -68,15 +68,6 @@ class DatabaseFeatures(BaseDatabaseFeatures): "aggregation.tests.AggregateTestCase.test_reverse_fkey_annotate", "aggregation_regress.tests.AggregationTests.test_annotation_disjunction", "aggregation_regress.tests.AggregationTests.test_decimal_aggregate_annotation_filter", - # QuerySet.extra(select=...) should raise NotSupportedError instead of: - # 'RawSQL' object has no attribute 'as_mql'. - "aggregation_regress.tests.AggregationTests.test_annotate_with_extra", - "aggregation_regress.tests.AggregationTests.test_annotation", - "aggregation_regress.tests.AggregationTests.test_more_more3", - "aggregation_regress.tests.AggregationTests.test_more_more_more3", - # QuerySet.extra(where=...) should raise NotSupportedError instead of: - # 'ExtraWhere' object has no attribute 'as_mql'. - "many_to_one.tests.ManyToOneTests.test_selects", # Incorrect JOIN with GenericRelation gives incorrect results. "aggregation_regress.tests.AggregationTests.test_aggregation_with_generic_reverse_relation", "generic_relations.tests.GenericRelationsTests.test_queries_content_type_restriction", @@ -484,6 +475,7 @@ def django_test_expected_failures(self): "delete_regress.tests.Ticket19102Tests.test_ticket_19102_extra", "lookup.tests.LookupTests.test_values", "lookup.tests.LookupTests.test_values_list", + "many_to_one.tests.ManyToOneTests.test_selects", "ordering.tests.OrderingTests.test_extra_ordering", "ordering.tests.OrderingTests.test_extra_ordering_quoting", "ordering.tests.OrderingTests.test_extra_ordering_with_table_name", @@ -519,6 +511,10 @@ def django_test_expected_failures(self): }, "Test executes raw SQL.": { "aggregation.tests.AggregateTestCase.test_coalesced_empty_result_set", + "aggregation_regress.tests.AggregationTests.test_annotate_with_extra", + "aggregation_regress.tests.AggregationTests.test_annotation", + "aggregation_regress.tests.AggregationTests.test_more_more3", + "aggregation_regress.tests.AggregationTests.test_more_more_more3", "annotations.tests.NonAggregateAnnotationTestCase.test_raw_sql_with_inherited_field", "backends.base.test_base.ExecuteWrapperTests", "backends.tests.BackendTestCase.test_cursor_contextmanager", diff --git a/django_mongodb/query.py b/django_mongodb/query.py index 25d857bc1..df2d0d19c 100644 --- a/django_mongodb/query.py +++ b/django_mongodb/query.py @@ -2,13 +2,13 @@ from operator import add as add_operator from django.core.exceptions import EmptyResultSet, FullResultSet -from django.db import DatabaseError, IntegrityError +from django.db import DatabaseError, IntegrityError, NotSupportedError from django.db.models.expressions import Case, When from django.db.models.functions import Mod from django.db.models.lookups import Exact from django.db.models.sql.constants import INNER from django.db.models.sql.datastructures import Join -from django.db.models.sql.where import AND, OR, XOR, NothingNode, WhereNode +from django.db.models.sql.where import AND, OR, XOR, ExtraWhere, NothingNode, WhereNode from pymongo.errors import DuplicateKeyError, PyMongoError @@ -92,6 +92,10 @@ def get_pipeline(self): return pipeline +def extra_where(self, compiler, connection): # noqa: ARG001 + raise NotSupportedError("QuerySet.extra() is not supported on MongoDB.") + + def join(self, compiler, connection): lookup_pipeline = [] lhs_fields = [] @@ -232,6 +236,7 @@ def where_node(self, compiler, connection): def register_nodes(): + ExtraWhere.as_mql = extra_where Join.as_mql = join NothingNode.as_mql = NothingNode.as_sql WhereNode.as_mql = where_node