#33091: A FieldError should be raised when trying to update MTI inherited field
with F reference to child field
-------------------------------------+-------------------------------------
Reporter: Shai Berger | Owner: Clifford
Type: | Gama
Cleanup/optimization | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Clifford Gama):
* owner: Abhijith Ganesh => Clifford Gama
Comment:
I wonder if it would be acceptable to validate the expression references
in `UpdateQuery.add_update_values()` with something like:
{{{#!diff
diff --git a/django/db/models/options.py b/django/db/models/options.py
index 11b2742f7d..8457d74ff9 100644
--- a/django/db/models/options.py
+++ b/django/db/models/options.py
@@ -1018,6 +1018,18 @@ class Options:
names.append(field.attname)
return frozenset(names)
+ @cached_property
+ def _local_concrete_field_names(self):
+ """
+ Return a set of the local concrete field names defined on the
model.
+ """
+ names = []
+ for f in self.local_concrete_fields:
+ names.append(f.name)
+ if f.name != f.attname:
+ names.append(f.attname)
+ return frozenset(names)
+
@cached_property
def _reverse_one_to_one_field_names(self):
"""
diff --git a/django/db/models/sql/subqueries.py
b/django/db/models/sql/subqueries.py
index 9cb971b38f..7f6d06ee1f 100644
--- a/django/db/models/sql/subqueries.py
+++ b/django/db/models/sql/subqueries.py
@@ -99,6 +99,12 @@ class UpdateQuery(Query):
"Cannot update model field %r (only non-relations and
"
"foreign keys permitted)." % field
)
+ for field_name, *lookups in model._get_expr_references(val):
+ if field_name not in
model._meta._local_concrete_field_names:
+ raise FieldError(
+ "Cannot update model field %r using the non-local
field "
+ "reference %r." % (field, field_name)
+ )
if model is not self.get_meta().concrete_model:
self.add_related_update(model, field, val)
continue
}}}
this would handle the case in this ticket and the one in #30044 without
the need for special-casing either. This would make this ticket easier, by
simplifying the logic since we don't have to check whether a descendant's
reference is actually a field in the model originating the query update.
The downside is that this would also raise for invalid references, without
listing the available field choices.
--
Ticket URL: <https://code.djangoproject.com/ticket/33091#comment:10>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/django-updates/01070195f14bbffb-7ef09d26-8e88-4d51-863c-f3193909365c-000000%40eu-central-1.amazonses.com.