Browse files

Fixed #17502 -- Made joining in inheritance cases consistent

The original problem was that when filtering two levels up in
inheritance chain, Django optimized the join generation so that the
middle model was skipped. But then Django generated joins from top
to middle to bottom for SELECT clause, and thus there was one extra
join (top->middle->bottom + top -> bottom).

This case is fixed in master as the filtering optimization is gone.
This has the side effect that in some cases there is still extra join
if the SELECT clause doesn't contain anything from middle or bottom.
  • Loading branch information...
1 parent 3f2befc commit 2a2708e1b2a0e02fc4fdd4b050d60fb0033dfdde @akaariai akaariai committed Mar 15, 2013
Showing with 17 additions and 0 deletions.
  1. +17 −0 tests/model_inheritance_regress/
17 tests/model_inheritance_regress/
@@ -8,6 +8,7 @@
from django import forms
from django.test import TestCase
+from django.utils.unittest import expectedFailure
from .models import (Place, Restaurant, ItalianRestaurant, ParkingLot,
ParkingLot2, ParkingLot3, Supplier, Wholesaler, Child, SelfRefParent,
@@ -422,3 +423,19 @@ class Meta:
form = ProfileForm({'username': "user_with_profile", 'extra': "hello"},
+ def test_inheritance_joins(self):
+ # Test for #17502 - check that filtering through two levels of
+ # inheritance chain doesn't generate extra joins.
+ qs = ItalianRestaurant.objects.all()
+ self.assertEqual(str(qs.query).count('JOIN'), 2)
+ qs = ItalianRestaurant.objects.filter(name='foo')
+ self.assertEqual(str(qs.query).count('JOIN'), 2)
+ @expectedFailure
+ def test_inheritance_values_joins(self):
+ # It would be nice (but not too important) to skip the middle join in
+ # this case. Skipping is possible as nothing from the middle model is
+ # used in the qs and top contains direct pointer to the bottom model.
+ qs = ItalianRestaurant.objects.values_list('serves_gnocchi').filter(name='foo')
+ self.assertEqual(str(qs.query).count('JOIN'), 1)

0 comments on commit 2a2708e

Please sign in to comment.