#34612 closed Bug (fixed)
QuerySet.only() doesn't work with select_related() on a reverse OneToOneField relation.
Reported by: | Ian Cubitt | Owned by: | Simon Charette |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 4.2 |
Severity: | Release blocker | Keywords: | |
Cc: | Simon Charette | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
On Django 4.2 calling only()
with select_related()
on a query using the reverse lookup for a OneToOne relation does not generate the correct query.
All the fields from the related model are still included in the generated SQL.
Sample models:
class Main(models.Model): main_field_1 = models.CharField(blank=True, max_length=45) main_field_2 = models.CharField(blank=True, max_length=45) main_field_3 = models.CharField(blank=True, max_length=45) class Secondary(models.Model): main = models.OneToOneField(Main, primary_key=True, related_name='secondary', on_delete=models.CASCADE) secondary_field_1 = models.CharField(blank=True, max_length=45) secondary_field_2 = models.CharField(blank=True, max_length=45) secondary_field_3 = models.CharField(blank=True, max_length=45)
Sample code:
Main.objects.select_related('secondary').only('main_field_1', 'secondary__secondary_field_1')
Generated query on Django 4.2.1:
SELECT "bugtest_main"."id", "bugtest_main"."main_field_1", "bugtest_secondary"."main_id", "bugtest_secondary"."secondary_field_1", "bugtest_secondary"."secondary_field_2", "bugtest_secondary"."secondary_field_3" FROM "bugtest_main" LEFT OUTER JOIN "bugtest_secondary" ON ("bugtest_main"."id" = "bugtest_secondary"."main_id")
Generated query on Django 4.1.9:
SELECT "bugtest_main"."id", "bugtest_main"."main_field_1", "bugtest_secondary"."main_id", "bugtest_secondary"."secondary_field_1" FROM "bugtest_main" LEFT OUTER JOIN "bugtest_secondary" ON ("bugtest_main"."id" = "bugtest_secondary"."main_id")
Change History (7)
comment:1 by , 19 months ago
Cc: | added |
---|---|
Severity: | Normal → Release blocker |
Summary: | QuerySet.only() regression on Django 4.2 with select_related() on a reverse OneToOneField relation → QuerySet.only() doesn't work with select_related() on a reverse OneToOneField relation. |
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 19 months ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:3 by , 19 months ago
Has patch: | set |
---|
Note:
See TracTickets
for help on using tickets.
Thanks for the report!
Regression in b3db6c8dcb5145f7d45eff517bcd96460475c879.
Reproduced at 881cc139e2d53cc1d3ccea7f38faa960f9e56597.