From a36f3f4cc1b4540a243091356f86dc984ec2d80b Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Wed, 24 Jul 2024 14:01:24 -0400 Subject: [PATCH] fix QuerySet.update() with multi-table inheritance --- django_mongodb/compiler.py | 17 +++++++++++++++-- django_mongodb/features.py | 3 --- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/django_mongodb/compiler.py b/django_mongodb/compiler.py index 80751ca0..6afa630e 100644 --- a/django_mongodb/compiler.py +++ b/django_mongodb/compiler.py @@ -324,8 +324,14 @@ def check_query(self): ) -class SQLUpdateCompiler(SQLCompiler): +class SQLUpdateCompiler(compiler.SQLUpdateCompiler, SQLCompiler): def execute_sql(self, result_type): + """ + Execute the specified update. Return the number of rows affected by + the primary update query. The "primary update query" is the first + non-empty query that is executed. Row counts for any subsequent, + related queries are not available. + """ self.pre_sql_setup() values = [] for field, _, value in self.query.values: @@ -340,7 +346,14 @@ def execute_sql(self, result_type): ) prepared = field.get_db_prep_save(value, connection=self.connection) values.append((field, prepared)) - return self.update(values) + is_empty = not bool(values) + rows = 0 if is_empty else self.update(values) + for query in self.query.get_related_updates(): + aux_rows = query.get_compiler(self.using).execute_sql(result_type) + if is_empty and aux_rows: + rows = aux_rows + is_empty = False + return rows def update(self, values): spec = {} diff --git a/django_mongodb/features.py b/django_mongodb/features.py index 61d968d1..3b8dee66 100644 --- a/django_mongodb/features.py +++ b/django_mongodb/features.py @@ -51,9 +51,6 @@ class DatabaseFeatures(BaseDatabaseFeatures): "update.tests.AdvancedTests.test_update_ordered_by_inline_m2m_annotation", "update.tests.AdvancedTests.test_update_ordered_by_m2m_annotation", "update.tests.AdvancedTests.test_update_ordered_by_m2m_annotation_desc", - # pymongo: ValueError: update cannot be empty - "update.tests.SimpleTest.test_empty_update_with_inheritance", - "update.tests.SimpleTest.test_nonempty_update_with_inheritance", # Pattern lookups that use regexMatch don't work on JSONField: # Unsupported conversion from array to string in $convert "model_fields.test_jsonfield.TestQuerying.test_icontains",