[Django] #33608: Wrong sql generation for FK with db_constraint=False causing results to incorrectly get dropped from a queryset

7 views
Skip to first unread message

Django

unread,
Mar 30, 2022, 7:16:52 AM3/30/22
to django-...@googlegroups.com
#33608: Wrong sql generation for FK with db_constraint=False causing results to
incorrectly get dropped from a queryset
-------------------------------------+-------------------------------------
Reporter: Mathieu | Owner: nobody
Hinderyckx |
Type: Bug | Status: new
Component: Database | Version: 4.0
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 |
-------------------------------------+-------------------------------------
When using a FK which has db_constraint=False, the sql generated when
using the combination of a filter() and select_related() are wrong,
causing results which should be included in a queryset to not be.

The problem occurs when the remote object does not exist in the table
where the FK points to. In some cases the ORM will generate an `INNER
JOIN` statement instead of a `LEFT OUTER JOIN` causing a row to be wrongly
omitted.

I have made a POC setup for this behaviour on
https://github.com/mhindery/django_fk_issue, with testcases containing
some working lookups, and one specific method that demonstrate the
behaviour. I've included in comments in the tests the query which gets
generated, showing the problematic join.

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

Django

unread,
Mar 30, 2022, 7:23:20 AM3/30/22
to django-...@googlegroups.com
#33608: Wrong sql generation for FK with db_constraint=False causing results to
incorrectly get dropped from a queryset
-------------------------------------+-------------------------------------
Reporter: Mathieu Hinderyckx | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mathieu Hinderyckx):

* version: 4.0 => 3.2


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

Reply all
Reply to author
Forward
0 new messages