Skip to content

fix QuerySet.update() with multi-table inheritance #87

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 29, 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
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