Opened 7 years ago

Closed 7 years ago

Last modified 5 years ago

#12240 closed (fixed)

select_related doesn't work correctly when mixing nullable and non-nullable keys

Reported by: Michał Bartoszkiewicz Owned by: nobody
Component: Database layer (models, ORM) Version: 1.1
Severity: Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

The following models:

class Model1(models.Model):
    x = models.ForeignKey('Model2', null=True)

class Model2(models.Model):
    y = models.ForeignKey('Model3', null=False)

class Model3(models.Model):
    z = models.ForeignKey('Model4', null=False)

class Model4(models.Model):
    q = models.IntegerField()

cause a problem with select_related():

In [4]: models.Model1.objects.select_related('x__y')
Out[4]: [<Model1: Model1 object>]

In [5]: models.Model1.objects.select_related('x__y__z')
Out[5]: []

It look like the fix for #7369 only fixed a case when a single non-nullable ForeignKey is encountered, not when there are two in a row.

Attachments (2)

django-12240.diff (2.2 KB) - added by Michał Bartoszkiewicz 7 years ago.
django-select-related-join-chain.diff (2.5 KB) - added by Alex Gaynor 7 years ago.

Download all attachments as: .zip

Change History (8)

Changed 7 years ago by Michał Bartoszkiewicz

Attachment: django-12240.diff added

comment:1 Changed 7 years ago by Michał Bartoszkiewicz

Has patch: set

The attached patch fixes the problem for me.

comment:2 Changed 7 years ago by Russell Keith-Magee

milestone: 1.2
Triage Stage: UnreviewedAccepted

Changed 7 years ago by Alex Gaynor

comment:3 Changed 7 years ago by Alex Gaynor

Triage Stage: AcceptedReady for checkin

comment:4 Changed 7 years ago by Luke Plant

Resolution: fixed
Status: newclosed

(In [12719]) Fixed #12240 - select_related doesn't work correctly when mixing nullable and non-nullable keys

Thanks to embe for report and Alex for fix.

comment:5 Changed 7 years ago by Luke Plant

(In [12720]) [1.1.X] Fixed #12240 - select_related doesn't work correctly when mixing nullable and non-nullable keys

Thanks to embe for report and Alex for fix.

Backport of 12719 from trunk.

comment:6 Changed 5 years ago by Jacob

milestone: 1.2

Milestone 1.2 deleted

Note: See TracTickets for help on using tickets.
Back to Top