Skip to content

Commit 835d50a

Browse files
committed
fix QuerySet.update() with multi-table inheritance
1 parent c09f258 commit 835d50a

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

django_mongodb/compiler.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,14 @@ def check_query(self):
323323
)
324324

325325

326-
class SQLUpdateCompiler(SQLCompiler):
326+
class SQLUpdateCompiler(compiler.SQLUpdateCompiler, SQLCompiler):
327327
def execute_sql(self, result_type):
328+
"""
329+
Execute the specified update. Return the number of rows affected by
330+
the primary update query. The "primary update query" is the first
331+
non-empty query that is executed. Row counts for any subsequent,
332+
related queries are not available.
333+
"""
328334
self.pre_sql_setup()
329335
values = []
330336
for field, _, value in self.query.values:
@@ -339,7 +345,14 @@ def execute_sql(self, result_type):
339345
)
340346
prepared = field.get_db_prep_save(value, connection=self.connection)
341347
values.append((field, prepared))
342-
return self.update(values)
348+
is_empty = not bool(values)
349+
rows = 0 if is_empty else self.update(values)
350+
for query in self.query.get_related_updates():
351+
aux_rows = query.get_compiler(self.using).execute_sql(result_type)
352+
if is_empty and aux_rows:
353+
rows = aux_rows
354+
is_empty = False
355+
return rows
343356

344357
def update(self, values):
345358
spec = {}

django_mongodb/features.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
5151
"update.tests.AdvancedTests.test_update_ordered_by_inline_m2m_annotation",
5252
"update.tests.AdvancedTests.test_update_ordered_by_m2m_annotation",
5353
"update.tests.AdvancedTests.test_update_ordered_by_m2m_annotation_desc",
54-
# pymongo: ValueError: update cannot be empty
55-
"update.tests.SimpleTest.test_empty_update_with_inheritance",
56-
"update.tests.SimpleTest.test_nonempty_update_with_inheritance",
5754
# Pattern lookups that use regexMatch don't work on JSONField:
5855
# Unsupported conversion from array to string in $convert
5956
"model_fields.test_jsonfield.TestQuerying.test_icontains",

0 commit comments

Comments
 (0)