Skip to content

Commit fd72adc

Browse files
WaVEVtimgraham
authored andcommitted
fix isnull lookup when querying joined tables
1 parent 8bc7160 commit fd72adc

File tree

2 files changed

+13
-20
lines changed

2 files changed

+13
-20
lines changed

django_mongodb/base.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,26 @@ class DatabaseWrapper(BaseDatabaseWrapper):
8282
"endswith": "LIKE '%%' || {}",
8383
"iendswith": "LIKE '%%' || UPPER({})",
8484
}
85+
86+
def _isnull_operator(a, b):
87+
is_null = {
88+
"$or": [
89+
# The path does not exist (i.e. is "missing")
90+
{"$eq": [{"$type": a}, "missing"]},
91+
# or the value is None.
92+
{"$eq": [a, None]},
93+
]
94+
}
95+
return is_null if b else {"$not": is_null}
96+
8597
mongo_operators = {
8698
"exact": lambda a, b: {"$eq": [a, b]},
8799
"gt": lambda a, b: {"$gt": [a, b]},
88100
"gte": lambda a, b: {"$gte": [a, b]},
89101
"lt": lambda a, b: {"$lt": [a, b]},
90102
"lte": lambda a, b: {"$lte": [a, b]},
91103
"in": lambda a, b: {"$in": [a, b]},
92-
"isnull": lambda a, b: {("$eq" if b else "$ne"): [a, None]},
104+
"isnull": _isnull_operator,
93105
"range": lambda a, b: {"$and": [{"$gte": [a, b[0]]}, {"$lte": [a, b[1]]}]},
94106
"iexact": lambda a, b: regex_match(a, ("^", b, {"$literal": "$"}), insensitive=True),
95107
"startswith": lambda a, b: regex_match(a, ("^", b)),

django_mongodb/features.py

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,34 +77,18 @@ class DatabaseFeatures(BaseDatabaseFeatures):
7777
# decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]
7878
"lookup.tests.LookupTests.test_lookup_rhs",
7979
# Wrong results in queries with multiple tables.
80-
"annotations.tests.NonAggregateAnnotationTestCase.test_annotation_aggregate_with_m2o",
8180
"annotations.tests.NonAggregateAnnotationTestCase.test_annotation_reverse_m2m",
8281
"annotations.tests.NonAggregateAnnotationTestCase.test_annotation_with_m2m",
8382
"annotations.tests.NonAggregateAnnotationTestCase.test_chaining_annotation_filter_with_m2m",
8483
"annotations.tests.NonAggregateAnnotationTestCase.test_mti_annotations",
8584
"expressions.test_queryset_values.ValuesExpressionsTests.test_values_list_expression",
8685
"expressions.test_queryset_values.ValuesExpressionsTests.test_values_list_expression_flat",
8786
"expressions.tests.IterableLookupInnerExpressionsTests.test_expressions_in_lookups_join_choice",
88-
"expressions_case.tests.CaseExpressionTests.test_join_promotion",
89-
"expressions_case.tests.CaseExpressionTests.test_join_promotion_multiple_annotations",
9087
"ordering.tests.OrderingTests.test_order_by_grandparent_fk_with_expression_in_default_ordering",
9188
"ordering.tests.OrderingTests.test_order_by_parent_fk_with_expression_in_default_ordering",
9289
"ordering.tests.OrderingTests.test_order_by_ptr_field_with_default_ordering_by_expression",
93-
"queries.tests.NullJoinPromotionOrTest.test_isnull_filter_promotion",
94-
"queries.tests.NullJoinPromotionOrTest.test_ticket_21366",
95-
"queries.tests.NullJoinPromotionOrTest.test_ticket_21748",
9690
"queries.tests.Queries1Tests.test_order_by_tables",
97-
"queries.tests.Queries1Tests.test_ticket1050",
98-
"queries.tests.Queries1Tests.test_ticket2400",
9991
"queries.tests.Queries1Tests.test_ticket4358",
100-
"queries.tests.Queries1Tests.test_ticket_10790_1",
101-
"queries.tests.Queries1Tests.test_ticket_10790_4",
102-
"queries.tests.Queries1Tests.test_ticket_10790_6",
103-
"queries.tests.Queries1Tests.test_ticket_10790_7",
104-
"queries.tests.Queries4Tests.test_ticket15316_exclude_false",
105-
"queries.tests.Queries4Tests.test_ticket15316_filter_true",
106-
"queries.tests.Queries4Tests.test_ticket15316_one2one_exclude_false",
107-
"queries.tests.Queries4Tests.test_ticket15316_one2one_filter_true",
10892
"queries.tests.Queries4Tests.test_ticket7095",
10993
"queries.tests.TestTicket24605.test_ticket_24605",
11094
"queries.tests.TestInvalidValuesRelation.test_invalid_values",
@@ -113,9 +97,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
11397
"annotations.tests.AliasTests.test_order_by_alias_aggregate",
11498
# annotate() + values_list() + order_by() loses annotated value.
11599
"expressions_case.tests.CaseExpressionTests.test_annotate_values_not_in_order_by",
116-
# Querying the reverse side of a foreign key for None returns no
117-
# results: https://github.com/mongodb-labs/django-mongodb/issues/76
118-
"one_to_one.tests.OneToOneTests.test_filter_one_to_one_relations",
119100
# pymongo.errors.OperationFailure: the limit must be positive
120101
"queries.tests.WeirdQuerysetSlicingTests.test_tickets_7698_10202",
121102
# QuerySet.explain() not implemented:

0 commit comments

Comments
 (0)