Skip to content

Commit

Permalink
add NotSupportedError for update queries that use multiple collections
Browse files Browse the repository at this point in the history
  • Loading branch information
timgraham committed Jul 30, 2024
1 parent 638e95c commit 43a3345
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
7 changes: 7 additions & 0 deletions django_mongodb/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 4 additions & 3 deletions django_mongodb/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 = {
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 43a3345

Please sign in to comment.