diff options
| author | Simon Charette <charette.s@gmail.com> | 2025-05-08 14:02:35 -0400 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2025-05-12 12:33:07 +0100 |
| commit | 8be0c0d6901669661fca578f474cd51cd284d35a (patch) | |
| tree | 9a0caf26bfa3e6aa3d92064433186c8524f0a8bc /django | |
| parent | 42ab99309d347f617d60751c2e8d627fb2963049 (diff) | |
Fixed #36373 -- Fixed select_related() crash on foreign object for a composite pk.
Thanks Jacob Walls for the report and Sarah for the in-depth review.
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/models/query.py | 6 |
1 files changed, 5 insertions, 1 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py index 4ba96928d4..663f8bade4 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -2678,7 +2678,11 @@ class RelatedPopulator: ) self.model_cls = klass_info["model"] - self.pk_idx = self.init_list.index(self.model_cls._meta.pk.attname) + # A primary key must have all of its constituents not-NULL as + # NULL != NULL and thus NULL cannot be referenced through a foreign + # relationship. Therefore checking for a single member of the primary + # key is enough to determine if the referenced object exists or not. + self.pk_idx = self.init_list.index(self.model_cls._meta.pk_fields[0].attname) self.related_populators = get_related_populators(klass_info, select, self.db) self.local_setter = klass_info["local_setter"] self.remote_setter = klass_info["remote_setter"] |
