Skip to content

Commit 43a3345

Browse files
committed
add NotSupportedError for update queries that use multiple collections
1 parent 638e95c commit 43a3345

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,13 @@ Migrations for 'admin':
113113
- `bulk_update()`
114114
- `dates()`
115115
- `datetimes()`
116-
- `delete()`, if the query uses multiple collections.
117116
- `distinct()`
118117
- `extra()`
119118
- `prefetch_related()`
120119

120+
- `QuerySet.delete()` and `update()` do not support queries that span multiple
121+
collections.
122+
121123
- `Subquery`, `Exists`, and using a `QuerySet` in `QuerySet.annotate()` aren't
122124
supported.
123125

django_mongodb/compiler.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,13 @@ def execute_update(self, update_spec, **kwargs):
377377
options = dict(options, **kwargs)
378378
return collection.update_many(criteria, update_spec, **options).matched_count
379379

380+
def check_query(self):
381+
super().check_query()
382+
if len([a for a in self.query.alias_map if self.query.alias_refcount[a]]) > 1:
383+
raise NotSupportedError(
384+
"Cannot use QuerySet.update() when querying across multiple collections on MongoDB."
385+
)
386+
380387

381388
class SQLAggregateCompiler(SQLCompiler):
382389
pass

django_mongodb/features.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
8383
"ordering.tests.OrderingTests.test_order_by_ptr_field_with_default_ordering_by_expression",
8484
"queries.tests.Queries1Tests.test_order_by_tables",
8585
"queries.tests.Queries1Tests.test_ticket4358",
86-
"queries.tests.Queries4Tests.test_ticket7095",
8786
"queries.tests.TestTicket24605.test_ticket_24605",
8887
"queries.tests.TestInvalidValuesRelation.test_invalid_values",
8988
# alias().order_by() doesn't work.
@@ -96,8 +95,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
9695
# QuerySet.explain() not implemented:
9796
# https://github.com/mongodb-labs/django-mongodb/issues/28
9897
"queries.test_explain.ExplainUnsupportedTests.test_message",
99-
# filter() on related model + update() doesn't work.
100-
"queries.tests.Queries5Tests.test_ticket9848",
10198
}
10299
# $bitAnd, #bitOr, and $bitXor are new in MongoDB 6.3.
103100
_django_test_expected_failures_bitwise = {
@@ -415,6 +412,10 @@ def django_test_expected_failures(self):
415412
"delete_regress.tests.Ticket19102Tests.test_ticket_19102_select_related",
416413
"one_to_one.tests.OneToOneTests.test_o2o_primary_key_delete",
417414
},
415+
"Cannot use QuerySet.update() when querying across multiple collections on MongoDB.": {
416+
"queries.tests.Queries4Tests.test_ticket7095",
417+
"queries.tests.Queries5Tests.test_ticket9848",
418+
},
418419
"QuerySet.dates() is not supported on MongoDB.": {
419420
"annotations.tests.AliasTests.test_dates_alias",
420421
"dates.tests.DatesTests.test_dates_trunc_datetime_fields",

0 commit comments

Comments
 (0)