#34612: QuerySet.only() regression on Django 4.2 with select_related() on a
reverse
OneToOneField relation
-------------------------------------+-------------------------------------
Reporter: Ian | Owner: nobody
Cubitt |
Type: Bug | Status: new
Component: Database | Version: 4.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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:
{{{
#!python
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:
{{{
#!python
Main.objects.select_related('secondary').only('main_field_1',
'secondary__secondary_field_1')
}}}
Generated query on Django 4.2.1:
{{{
#!sql
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:
{{{
#!sql
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")
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34612>
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 on the web visit
https://groups.google.com/d/msgid/django-updates/01070188709c23f9-bea7a09a-2b47-4300-b36c-2b38030915c9-000000%40eu-central-1.amazonses.com.