diff options
| author | Simon Charette <charette.s@gmail.com> | 2025-01-13 13:55:41 -0500 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2025-01-14 16:21:19 +0100 |
| commit | 161e79d277ffe8b79b15ad51cb0d23de54270202 (patch) | |
| tree | 5a6f24da04f105288819d04697333591064185aa | |
| parent | 4bfec242b488b174f7d386ab5bbd3363751cdb93 (diff) | |
Refs #36075 -- Adjusted pk_fields usage in bulk_update eligibility checks.
Regression in bf7b17d16d3978b2e1cee4a0f7ce8840bd1a8dc4.
Thanks Sage Abdullah for the report.
| -rw-r--r-- | django/db/models/query.py | 5 | ||||
| -rw-r--r-- | tests/queries/test_bulk_update.py | 4 |
2 files changed, 8 insertions, 1 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py index 436233336d..25995b0d83 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -878,7 +878,10 @@ class QuerySet(AltersData): fields = [self.model._meta.get_field(name) for name in fields] if any(not f.concrete or f.many_to_many for f in fields): raise ValueError("bulk_update() can only be used with concrete fields.") - if any(f in self.model._meta.pk_fields for f in fields): + all_pk_fields = set(self.model._meta.pk_fields) + for parent in self.model._meta.all_parents: + all_pk_fields.update(parent._meta.pk_fields) + if any(f in all_pk_fields for f in fields): raise ValueError("bulk_update() cannot be used with primary key fields.") if not objs: return 0 diff --git a/tests/queries/test_bulk_update.py b/tests/queries/test_bulk_update.py index b2688a61c8..9fa9c3b9b8 100644 --- a/tests/queries/test_bulk_update.py +++ b/tests/queries/test_bulk_update.py @@ -144,6 +144,10 @@ class BulkUpdateTests(TestCase): with self.assertRaisesMessage(ValueError, self.pk_fields_error): CustomPk.objects.bulk_update([], ["name"]) + def test_update_inherited_primary_key(self): + with self.assertRaisesMessage(ValueError, self.pk_fields_error): + SpecialCategory.objects.bulk_update([], ["id"]) + def test_empty_objects(self): with self.assertNumQueries(0): rows_updated = Note.objects.bulk_update([], ["note"]) |
