[Django] #34321: Unexpected query result after call QuerySet.only() to ForeignKey() with 'self' reference

4 views
Skip to first unread message

Django

unread,
Feb 8, 2023, 4:43:48 AM2/8/23
to django-...@googlegroups.com
#34321: Unexpected query result after call QuerySet.only() to ForeignKey() with
'self' reference
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
Mahardhika Praja Taruna Negara |
Type: Bug | Status: new
Component: Database | Version: 3.2
layer (models, ORM) |
Severity: Normal | Keywords: queryset
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Unexpected query result after calling `QuerySet.only('ref__field')` to
`ForeignKey('self')`

{{{#!python
class MyModel(models.Model):
f1 = models.IntegerField()
f2 = models.IntegerField()
f3 = models.IntegerField()
ref = models.ForeignKey("self", on_delete=models.CASCADE)

print(str(MyModel.objects.select_related('ref').only('ref__f1').query))
}}}

Output
{{{
'SELECT "mymodel"."id", "mymodel"."f1", "mymodel"."ref_id", T2."id",
T2."f1", T2."ref_id" FROM "mymodel" INNER JOIN "mymodel" T2 ON
("mymodel"."ref_id" = T2."id")'
}}}

Fields `"mymodel"."f2"` and `"mymodel"."f3"` are excluded from column
selection

--
Ticket URL: <https://code.djangoproject.com/ticket/34321>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 8, 2023, 4:48:53 AM2/8/23
to django-...@googlegroups.com
#34321: Unexpected query result after call QuerySet.only() to ForeignKey() with
'self' reference
-------------------------------------+-------------------------------------
Reporter: Mahardhika Praja | Owner: nobody

Taruna Negara |
Type: Bug | Status: new
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: queryset | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Mahardhika Praja Taruna Negara:

Old description:

> Unexpected query result after calling `QuerySet.only('ref__field')` to
> `ForeignKey('self')`
>
> {{{#!python
> class MyModel(models.Model):
> f1 = models.IntegerField()
> f2 = models.IntegerField()
> f3 = models.IntegerField()
> ref = models.ForeignKey("self", on_delete=models.CASCADE)
>
> print(str(MyModel.objects.select_related('ref').only('ref__f1').query))
> }}}
>
> Output
> {{{
> 'SELECT "mymodel"."id", "mymodel"."f1", "mymodel"."ref_id", T2."id",
> T2."f1", T2."ref_id" FROM "mymodel" INNER JOIN "mymodel" T2 ON
> ("mymodel"."ref_id" = T2."id")'
> }}}
>
> Fields `"mymodel"."f2"` and `"mymodel"."f3"` are excluded from column
> selection

New description:

Unexpected query result after calling `QuerySet.only('ref__field')` to
`ForeignKey('self')`

{{{#!python
class MyModel(models.Model):
f1 = models.IntegerField()
f2 = models.IntegerField()
f3 = models.IntegerField()
ref = models.ForeignKey("self", on_delete=models.CASCADE)

print(str(MyModel.objects.select_related('ref').only('ref__f1').query))
}}}

Output
{{{
'SELECT "mymodel"."id", "mymodel"."f1", "mymodel"."ref_id", T2."id",
T2."f1", T2."ref_id" FROM "mymodel" INNER JOIN "mymodel" T2 ON
("mymodel"."ref_id" = T2."id")'
}}}

Fields `"mymodel"."f2"` and `"mymodel"."f3"` are excluded from column
selection

Another try

{{{#!python
print(str(MyModel.objects.select_related('ref').only('f1', 'f2', 'f3',
'ref__f1').query))
}}}

Output
{{{
'SELECT "mymodel"."id", "mymodel"."f1", "mymodel"."f2", "mymodel"."f3",
"mymodel"."ref_id", T2."id", T2."f1", T2."f2", T2."f3", T2."ref_id" FROM


"mymodel" INNER JOIN "mymodel" T2 ON ("mymodel"."ref_id" = T2."id")'
}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/34321#comment:1>

Django

unread,
Feb 8, 2023, 5:01:13 AM2/8/23
to django-...@googlegroups.com
#34321: Unexpected query result after call QuerySet.only() to ForeignKey() with
'self' reference
-------------------------------------+-------------------------------------
Reporter: Mahardhika Praja | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: invalid

Keywords: queryset | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => invalid


Comment:

`only()` works as
[https://docs.djangoproject.com/en/stable/ref/models/querysets/#only
documented] and expected, i.e. it replaces the set of fields to load
immediately, so in your case all columns `"mymodel"."f1"`,
`"mymodel"."f2"`, and `"mymodel"."f3"` from `MyModel` should be omitted in
the `SELECT` clause. `f1` appears because `MyModel` is involved more than
once, it was fixed in b3db6c8dcb5145f7d45eff517bcd96460475c879 (see
#21204).

--
Ticket URL: <https://code.djangoproject.com/ticket/34321#comment:2>

Reply all
Reply to author
Forward
0 new messages