From 43a3345b3473229d6bcae2f7aa0e78d61bc798ac Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Mon, 29 Jul 2024 20:23:48 -0400 Subject: [PATCH] add NotSupportedError for update queries that use multiple collections --- README.md | 4 +++- django_mongodb/compiler.py | 7 +++++++ django_mongodb/features.py | 7 ++++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cb88bffb..13000e8d 100644 --- a/README.md +++ b/README.md @@ -113,11 +113,13 @@ Migrations for 'admin': - `bulk_update()` - `dates()` - `datetimes()` - - `delete()`, if the query uses multiple collections. - `distinct()` - `extra()` - `prefetch_related()` +- `QuerySet.delete()` and `update()` do not support queries that span multiple + collections. + - `Subquery`, `Exists`, and using a `QuerySet` in `QuerySet.annotate()` aren't supported. diff --git a/django_mongodb/compiler.py b/django_mongodb/compiler.py index 6afa630e..b96e2732 100644 --- a/django_mongodb/compiler.py +++ b/django_mongodb/compiler.py @@ -377,6 +377,13 @@ def execute_update(self, update_spec, **kwargs): options = dict(options, **kwargs) return collection.update_many(criteria, update_spec, **options).matched_count + def check_query(self): + super().check_query() + if len([a for a in self.query.alias_map if self.query.alias_refcount[a]]) > 1: + raise NotSupportedError( + "Cannot use QuerySet.update() when querying across multiple collections on MongoDB." + ) + class SQLAggregateCompiler(SQLCompiler): pass diff --git a/django_mongodb/features.py b/django_mongodb/features.py index 2b1b17ab..4eaff0c2 100644 --- a/django_mongodb/features.py +++ b/django_mongodb/features.py @@ -83,7 +83,6 @@ class DatabaseFeatures(BaseDatabaseFeatures): "ordering.tests.OrderingTests.test_order_by_ptr_field_with_default_ordering_by_expression", "queries.tests.Queries1Tests.test_order_by_tables", "queries.tests.Queries1Tests.test_ticket4358", - "queries.tests.Queries4Tests.test_ticket7095", "queries.tests.TestTicket24605.test_ticket_24605", "queries.tests.TestInvalidValuesRelation.test_invalid_values", # alias().order_by() doesn't work. @@ -96,8 +95,6 @@ class DatabaseFeatures(BaseDatabaseFeatures): # QuerySet.explain() not implemented: # https://github.com/mongodb-labs/django-mongodb/issues/28 "queries.test_explain.ExplainUnsupportedTests.test_message", - # filter() on related model + update() doesn't work. - "queries.tests.Queries5Tests.test_ticket9848", } # $bitAnd, #bitOr, and $bitXor are new in MongoDB 6.3. _django_test_expected_failures_bitwise = { @@ -415,6 +412,10 @@ def django_test_expected_failures(self): "delete_regress.tests.Ticket19102Tests.test_ticket_19102_select_related", "one_to_one.tests.OneToOneTests.test_o2o_primary_key_delete", }, + "Cannot use QuerySet.update() when querying across multiple collections on MongoDB.": { + "queries.tests.Queries4Tests.test_ticket7095", + "queries.tests.Queries5Tests.test_ticket9848", + }, "QuerySet.dates() is not supported on MongoDB.": { "annotations.tests.AliasTests.test_dates_alias", "dates.tests.DatesTests.test_dates_trunc_datetime_fields",