Ticket #20528: 20528-test-edf93127bf2f9dc35b45cdea5d39a1b417ab1638.diff

File 20528-test-edf93127bf2f9dc35b45cdea5d39a1b417ab1638.diff, 2.5 KB (added by timo, 22 months ago)
  • tests/regressiontests/select_related_regress/models.py

    diff --git a/tests/regressiontests/select_related_regress/models.py b/tests/regressiontests/select_related_regress/models.py
    index a291a54..c6d4e1c 100644
    a b class Item(models.Model): 
    9494
    9595    def __str__(self):
    9696        return self.name
     97
     98class ChildA(models.Model):
     99    parent = models.ForeignKey(Parent)
     100
     101@python_2_unicode_compatible
     102class ChildB(models.Model):
     103    name = models.CharField(max_length=10)
     104    parent = models.ForeignKey(Parent, null=True)
     105    child = models.ForeignKey(ChildA, null=True)
     106
     107    def __str__(self):
     108        return self.name
  • tests/regressiontests/select_related_regress/tests.py

    diff --git a/tests/regressiontests/select_related_regress/tests.py b/tests/regressiontests/select_related_regress/tests.py
    index 0e27d07..47ef0ef 100644
    a b  
    11from __future__ import absolute_import, unicode_literals
    22
     3from django.db import models
    34from django.test import TestCase
    45from django.utils import six
    56
    67from .models import (Building, Child, Device, Port, Item, Country, Connection,
    78    ClientStatus, State, Client, SpecialClient, TUser, Person, Student,
    8     Organizer, Class, Enrollment)
     9    Organizer, Class, Enrollment, Parent, ChildA, ChildB)
    910
    1011
    1112class SelectRelatedRegressTests(TestCase):
    class SelectRelatedRegressTests(TestCase): 
    162163            # The select_related join was promoted as there is already an
    163164            # existing join.
    164165            self.assertTrue('LEFT OUTER' in str(qs.query))
     166
     167    def test_ticket_20528(self):
     168        """
     169        Regression for #20528
     170
     171        Using Qs and select_related with a certain config of ForeignKeys
     172        resulted in incorrect joins.
     173        """
     174        parent = Parent.objects.create(name="foo")
     175        childA = ChildA.objects.create(parent=parent)
     176        childB = ChildB.objects.create(child=childA, name="Bobby")
     177        queryset = ChildB.objects.filter(
     178            models.Q(parent=parent) | models.Q(child__parent=parent)
     179        )
     180        expected_queryset = ['<ChildB: Bobby>']
     181        # this always works
     182        self.assertQuerysetEqual(queryset, expected_queryset)
     183
     184        # this started failing after 3fef304ff237fe692459c1f5b840acf7886c50bb
     185        queryset = queryset.select_related('parent')
     186        import sys
     187        sys.stderr.write(str(queryset.query))
     188        self.assertQuerysetEqual(queryset, expected_queryset)
Back to Top