Skip to content

Commit

Permalink
fix QuerySet.update() with multi-table inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
timgraham committed Jul 25, 2024
1 parent a58f54c commit a36f3f4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
17 changes: 15 additions & 2 deletions django_mongodb/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 = {}
Expand Down
3 changes: 0 additions & 3 deletions django_mongodb/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit a36f3f4

Please sign in to comment.