Skip to content

add NotSupportedError for update queries that use multiple collections #91

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
Jul 30, 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
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 @@ -86,7 +86,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 @@ -99,8 +98,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 @@ -418,6 +415,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