diff --git a/django/db/models/sql/query.py b/django/db/models/sql/query.py
a
|
b
|
|
455 | 455 | if not rhs.alias_refcount[alias]: |
456 | 456 | # An unused alias. |
457 | 457 | continue |
| 458 | lhs_alias = rhs.alias_map[alias][LHS_ALIAS] |
| 459 | aux = (change_map.get(lhs_alias, lhs_alias),) + rhs.rev_join_map[alias][1:] |
458 | 460 | promote = (rhs.alias_map[alias][JOIN_TYPE] == self.LOUTER) |
459 | | new_alias = self.join(rhs.rev_join_map[alias], |
| 461 | new_alias = self.join(aux, |
460 | 462 | (conjunction and not first), used, promote, not conjunction) |
461 | 463 | used.add(new_alias) |
462 | 464 | change_map[alias] = new_alias |
diff --git a/tests/regressiontests/queries/models.py b/tests/regressiontests/queries/models.py
a
|
b
|
|
294 | 294 | |
295 | 295 | def __unicode__(self): |
296 | 296 | return u"%s" % self.num |
| 297 | |
| 298 | # Models for #10977 |
| 299 | |
| 300 | # A base class for Sponsor and Team |
| 301 | class Brand(models.Model): |
| 302 | pass |
| 303 | |
| 304 | class Mark(models.Model): |
| 305 | brand = models.ForeignKey(Brand) |
| 306 | name = models.CharField(max_length=20) |
| 307 | |
| 308 | class Sponsor(Brand): |
| 309 | pass |
| 310 | |
| 311 | class Team(Brand): |
| 312 | name = models.CharField(max_length=20) |
| 313 | sponsor = models.ForeignKey(Sponsor) |
| 314 | |
| 315 | def __unicode__(self): |
| 316 | return self.name |
diff --git a/tests/regressiontests/queries/tests.py b/tests/regressiontests/queries/tests.py
a
|
b
|
|
14 | 14 | from models import (Annotation, Article, Author, Celebrity, Child, Cover, Detail, |
15 | 15 | DumbCategory, ExtraInfo, Fan, Item, LeafA, LoopX, LoopZ, ManagedModel, |
16 | 16 | Member, NamedCategory, Note, Number, Plaything, PointerA, Ranking, Related, |
17 | | Report, ReservedName, Tag, TvChef, Valid, X, Food, Eaten, Node) |
| 17 | Report, ReservedName, Tag, TvChef, Valid, X, Food, Eaten, Node, |
| 18 | Sponsor, Team, Mark) |
18 | 19 | |
19 | 20 | |
20 | 21 | class BaseQuerysetTest(TestCase): |
… |
… |
|
1658 | 1659 | Number.objects.filter(num__in=numbers).count(), |
1659 | 1660 | 2500 |
1660 | 1661 | ) |
| 1662 | |
| 1663 | |
| 1664 | class QuerysetIntersectionTests(TestCase): |
| 1665 | """ |
| 1666 | Tests for merging querysets that have been filtered on multiple elements |
| 1667 | from the same base class |
| 1668 | """ |
| 1669 | |
| 1670 | def test_models_with_same_parent(self): |
| 1671 | s1 = Sponsor.objects.create() |
| 1672 | s2 = Sponsor.objects.create() |
| 1673 | Team.objects.create(sponsor=s1, name='Team A') |
| 1674 | Team.objects.create(sponsor=s2, name='Team B') |
| 1675 | Mark.objects.create(name='Product A', brand=s1) |
| 1676 | |
| 1677 | qs1 = Team.objects.all() |
| 1678 | |
| 1679 | qs2 = Team.objects.filter( |
| 1680 | Q(sponsor__mark__name='Product A') | |
| 1681 | Q(sponsor__mark__name='Product A') |
| 1682 | ) |
| 1683 | r = qs1 & qs2 |
| 1684 | self.assertQuerysetEqual(r, ['<Team: Team A>']) |