Index: modeltests/model_inheritance/tests.py
===================================================================
--- modeltests/model_inheritance/tests.py	(revision 0)
+++ modeltests/model_inheritance/tests.py	(revision 0)
@@ -0,0 +1,69 @@
+from django.contrib.auth.models import User
+from django.test import TestCase
+
+from modeltests.model_inheritance.models import (BasePost, PostSubclass,
+    AuthorProfile, STATUS_GOOD, STATUS_BAD)
+
+class SubclassExclusionTestCase(TestCase):
+    def setUp(self):
+        # create some users!
+        self.user_good = User.objects.create_user('good', 'good', 'good')
+        self.user_neither = User.objects.create_user('neither', 'neither', 'neither')
+        self.user_bad = User.objects.create_user('bad', 'bad', 'bad')
+        
+        # create profiles for good & bad users (but don't make one for neither
+        self.profile_good = AuthorProfile.objects.create(user=self.user_good, status=STATUS_GOOD)
+        self.profile_bad = AuthorProfile.objects.create(user=self.user_bad, status=STATUS_BAD)
+        
+        # create some posts for the users
+        # note that i'm creating them in a different order - this is because the
+        # inner query generates an incorrect join but can produce results that
+        # "look" correct because of the primary keys being used.  this ensures
+        # that the test will fail
+        self.post_bad1 = PostSubclass.objects.create(author=self.user_bad, title='Bad post1')
+        self.post_bad2 = PostSubclass.objects.create(author=self.user_bad, title='Bad post2')
+        self.post_good1 = PostSubclass.objects.create(author=self.user_good, title='Good post1')
+        self.post_good2 = PostSubclass.objects.create(author=self.user_good, title='Good post2')
+        self.post_neither1 = PostSubclass.objects.create(author=self.user_neither, title='Neither post1')
+        self.post_neither2 = PostSubclass.objects.create(author=self.user_neither, title='Neither post2')
+    
+    def test_filter_exclude(self):
+        '''
+        This is the query being generated by the .exclude() -- NOTE that lines
+        44-47 are basically saying "Give me posts where the author id is NOT
+        the base posts' id -- there's an extraneous join here that's messing
+        the query up.
+        
+        SELECT "model_inheritance_basepost"."id", "model_inheritance_basepost"."author_id", "model_inheritance_basepost"."title", "model_inheritance_postsubclass"."basepost_ptr_id" 
+        FROM "model_inheritance_postsubclass" 
+
+        INNER JOIN "model_inheritance_basepost" 
+            ON ("model_inheritance_postsubclass"."basepost_ptr_id" = "model_inheritance_basepost"."id") 
+
+        WHERE NOT ((
+            "model_inheritance_basepost"."author_id" IN (
+            
+                SELECT U1."id" FROM "model_inheritance_basepost" U1 
+                INNER JOIN "auth_user" U2 
+                    ON (U1."author_id" = U2."id") 
+                INNER JOIN "model_inheritance_authorprofile" U3 
+                    ON (U2."id" = U3."user_id") 
+                WHERE U3."status" = 2 
+            ) AND
+            "model_inheritance_basepost"."author_id" IS NOT NULL
+        )) 
+
+        ORDER BY "model_inheritance_postsubclass"."basepost_ptr_id" ASC
+        '''
+        # ensure that querying across joins works fine
+        good_post_qs = PostSubclass.objects.filter(
+            author__authorprofile__status=STATUS_GOOD
+        )
+        self.assertEqual(list(good_post_qs), [self.post_good1, self.post_good2])
+        
+        # this works all fine and well, but say we want to get all posts where
+        # the authors' status is anything *but* bad
+        not_bad_post_qs = PostSubclass.objects.exclude(
+            author__authorprofile__status=STATUS_BAD
+        )
+        self.assertEqual(list(not_bad_post_qs), [self.post_good1, self.post_good2, self.post_neither1, self.post_neither2])
Index: modeltests/model_inheritance/models.py
===================================================================
--- modeltests/model_inheritance/models.py	(revision 13617)
+++ modeltests/model_inheritance/models.py	(working copy)
@@ -12,6 +12,7 @@
 Both styles are demonstrated here.
 """
 
+from django.contrib.auth.models import User
 from django.db import models
 
 #
@@ -144,6 +145,32 @@
     def __unicode__(self):
         return self.content
 
+
+#
+# Incorrect joins being generated on an inner query by the .exclude() method
+#
+
+class BasePost(models.Model):
+    author = models.ForeignKey(User, related_name='posts')
+    title = models.CharField(max_length=100)
+    
+    class Meta:
+        ordering = ['id']
+
+class PostSubclass(BasePost):
+    pass
+
+STATUS_GOOD = 1
+STATUS_BAD = 2
+STATUS_CHOICES = (
+    (STATUS_GOOD, 'Good'),
+    (STATUS_BAD, 'Bad'),
+)
+class AuthorProfile(models.Model):
+    user = models.OneToOneField(User)
+    status = models.IntegerField(choices=STATUS_CHOICES)
+
+
 __test__ = {'API_TESTS':"""
 # The Student and Worker models both have 'name' and 'age' fields on them and
 # inherit the __unicode__() method, just as with normal Python subclassing.

