Skip to content

make QuerySet.extra(select=... and where=...) raise NotSupportedError #140

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions django_mongodb/expressions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
F,
NegatedExpression,
OrderBy,
RawSQL,
Ref,
ResolvedOuterRef,
Star,
Expand Down Expand Up @@ -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}."
Expand Down Expand Up @@ -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
Expand Down
14 changes: 5 additions & 9 deletions django_mongodb/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
9 changes: 7 additions & 2 deletions django_mongodb/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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 = []
Expand Down Expand Up @@ -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