diff -ruN -x settings.py -x '*.pyc' -x '*.stackdump' -x '*.orig' -x '*.rej' ../Django-1.4c1.orig/django/db/models/sql/query.py ../Django-1.4c1/django/db/models/sql/query.py
--- ../Django-1.4c1.orig/django/db/models/sql/query.py	2012-03-05 18:30:59.000000000 +0100
+++ ../Django-1.4c1/django/db/models/sql/query.py	2012-03-12 22:13:35.591785300 +0100
@@ -913,7 +913,10 @@
             # Not all tables need to be joined to anything. No join type
             # means the later columns are ignored.
             join_type = None
-        elif promote or outer_if_first:
+        elif promote or outer_if_first or \
+	  self.alias_map[connection[0]][JOIN_TYPE] == self.LOUTER:
+            # if preceeding alias (connection[0]) has LEFT OUTER JOIN
+            # we should promote it
             join_type = self.LOUTER
         else:
             join_type = self.INNER
diff -ruN -x settings.py -x '*.pyc' -x '*.stackdump' -x '*.orig' -x '*.rej' ../Django-1.4c1.orig/tests/regressiontests/queries/models.py ../Django-1.4c1/tests/regressiontests/queries/models.py
--- ../Django-1.4c1.orig/tests/regressiontests/queries/models.py	2012-03-05 18:30:57.000000000 +0100
+++ ../Django-1.4c1/tests/regressiontests/queries/models.py	2012-03-12 22:11:59.575184600 +0100
@@ -346,3 +346,16 @@
 
     def __unicode__(self):
         return "one2one " + self.new_name
+
+class DefinitionOfDoubleCategorizedObject(models.Model):
+    name = models.CharField(max_length=50)
+
+class CategorizedObject(models.Model):
+    name = models.CharField(max_length=50)
+    category = models.ForeignKey(SimpleCategory)
+
+class DoubleCategorizedObject(models.Model):
+    name = models.CharField(max_length=50)
+    rel_a = models.ForeignKey(CategorizedObject, null=True, blank=True, related_name="rel_a_double")
+    rel_b = models.ManyToManyField(CategorizedObject, null=True, blank=True, related_name="rel_b_double")
+    definition = models.ForeignKey(DefinitionOfDoubleCategorizedObject)
diff -ruN -x settings.py -x '*.pyc' -x '*.stackdump' -x '*.orig' -x '*.rej' ../Django-1.4c1.orig/tests/regressiontests/queries/tests.py ../Django-1.4c1/tests/regressiontests/queries/tests.py
--- ../Django-1.4c1.orig/tests/regressiontests/queries/tests.py	2012-03-05 18:30:57.000000000 +0100
+++ ../Django-1.4c1/tests/regressiontests/queries/tests.py	2012-03-12 22:14:21.974423100 +0100
@@ -3,6 +3,7 @@
 import datetime
 import pickle
 import sys
+import operator
 
 from django.conf import settings
 from django.core.exceptions import FieldError
@@ -18,7 +19,8 @@
     ManagedModel, Member, NamedCategory, Note, Number, Plaything, PointerA,
     Ranking, Related, Report, ReservedName, Tag, TvChef, Valid, X, Food, Eaten,
     Node, ObjectA, ObjectB, ObjectC, CategoryItem, SimpleCategory,
-    SpecialCategory, OneToOneCategory)
+    SpecialCategory, OneToOneCategory, DoubleCategorizedObject, CategorizedObject,
+    DefinitionOfDoubleCategorizedObject)
 
 
 class BaseQuerysetTest(TestCase):
@@ -1894,3 +1896,31 @@
             DumbCategory.objects.create()
         except TypeError:
             self.fail("Creation of an instance of a model with only the PK field shouldn't error out after bulk insert refactoring (#17056)")
+
+class Queries6Test(BaseQuerysetTest):
+    def setUp(self):
+        c1 = SimpleCategory.objects.create(name="category1")
+        c2 = SimpleCategory.objects.create(name="category2")
+	cat_obj1 = CategorizedObject.objects.create(category=c1, name='cat1')
+	cat_obj2 = CategorizedObject.objects.create(category=c2, name='cat2')
+	definition = DefinitionOfDoubleCategorizedObject()
+	definition.name = 'test def'
+	definition.save()
+	double_cat_obj = DoubleCategorizedObject()
+	double_cat_obj.name = "test obj"
+	double_cat_obj.rel_a = cat_obj1
+	double_cat_obj.definition = definition
+	double_cat_obj.save()
+    def test_or_query(self):
+        search_query = 'test def'
+        or_queries = [
+		Q(name__contains= search_query),
+		Q(doublecategorizedobject__rel_a__name__icontains= search_query),
+		Q(doublecategorizedobject__rel_a__category__name__icontains = search_query),
+		Q(doublecategorizedobject__rel_b__name__icontains= search_query),
+		Q(doublecategorizedobject__rel_b__category__name__icontains = search_query),
+	]
+    	q_obj = reduce(operator.or_, or_queries)
+	qset = DefinitionOfDoubleCategorizedObject.objects.filter(q_obj).select_related()
+	self.assertTrue(len(qset) == 1)
+	self.assertNotIn('INNER JOIN', str(qset.query))
