diff -ur Django-1.2.5/django/db/models/sql/compiler.py Django-1.2.5-patched/django/db/models/sql/compiler.py
--- Django-1.2.5/django/db/models/sql/compiler.py	2011-01-26 06:52:41.000000000 +0300
+++ Django-1.2.5-patched/django/db/models/sql/compiler.py	2011-06-21 14:26:54.019360014 +0300
@@ -449,13 +449,14 @@
                 result.append('%s%s%s' % (connector, qn(name), alias_str))
             first = False
         for t in self.query.extra_tables:
-            alias, unused = self.query.table_alias(t)
+            alias, unused = self.query.table_alias(t, True)
             # Only add the alias if it's not already present (the table_alias()
             # calls increments the refcount, so an alias refcount of one means
             # this is the only reference.
             if alias not in self.query.alias_map or self.query.alias_refcount[alias] == 1:
                 connector = not first and ', ' or ''
-                result.append('%s%s' % (connector, qn(alias)))
+                alias_str = (alias != t and ' %s' % alias or '')
+                result.append('%s%s%s' % (connector, qn(t), alias_str))
                 first = False
         return result, []
 
diff -ur Django-1.2.5/django/db/models/sql/query.py Django-1.2.5-patched/django/db/models/sql/query.py
--- Django-1.2.5/django/db/models/sql/query.py	2011-02-08 17:07:14.000000000 +0300
+++ Django-1.2.5-patched/django/db/models/sql/query.py	2011-06-21 13:10:22.619363387 +0300
@@ -629,7 +629,7 @@
 
         # Create a new alias for this table.
         if current:
-            alias = '%s%d' % (self.alias_prefix, len(self.alias_map) + 1)
+            alias = '%s%d' % (self.alias_prefix, sum([len(li) for li in self.table_map.values()]) + 1)
             current.append(alias)
         else:
             # The first occurence of a table uses the table name directly.
diff -ur Django-1.2.5/tests/regressiontests/queries/models.py Django-1.2.5-patched/tests/regressiontests/queries/models.py
--- Django-1.2.5/tests/regressiontests/queries/models.py	2011-01-25 06:39:38.000000000 +0300
+++ Django-1.2.5-patched/tests/regressiontests/queries/models.py	2011-06-21 13:12:27.615361481 +0300
@@ -294,3 +294,9 @@
 
     def __unicode__(self):
         return u"%s" % self.num
+    
+class NestedNode(models.Model):
+    lft = models.IntegerField()
+    rght = models.IntegerField()
+    name = models.CharField(max_length=100)
+    parent = models.ForeignKey("self", null=True)
\ No newline at end of file
diff -ur Django-1.2.5/tests/regressiontests/queries/tests.py Django-1.2.5-patched/tests/regressiontests/queries/tests.py
--- Django-1.2.5/tests/regressiontests/queries/tests.py	2011-01-25 06:39:38.000000000 +0300
+++ Django-1.2.5-patched/tests/regressiontests/queries/tests.py	2011-06-21 14:56:55.883360670 +0300
@@ -14,7 +14,7 @@
 from models import (Annotation, Article, Author, Celebrity, Child, Cover, Detail,
     DumbCategory, ExtraInfo, Fan, Item, LeafA, LoopX, LoopZ, ManagedModel,
     Member, NamedCategory, Note, Number, Plaything, PointerA, Ranking, Related,
-    Report, ReservedName, Tag, TvChef, Valid, X, Food, Eaten, Node)
+    Report, ReservedName, Tag, TvChef, Valid, X, Food, Eaten, Node, NestedNode)
 
 
 class BaseQuerysetTest(TestCase):
@@ -1650,3 +1650,45 @@
                 Number.objects.filter(num__in=numbers).count(),
                 2500
             )
+
+class ExtraTablesTest(TestCase):
+    def test_ticket12890(self):
+        """
+            This tests that self-joins, using extra(tables=...) work properly.
+            In the below example, a simple nested set model is used
+            (http://en.wikipedia.org/wiki/Nested_set_model), and the number
+            of children of each root node (including the node itself) are
+            calculated.
+            
+            If the aliasing doesn't work properly, this test case will fail
+            with an error at the commented line.  The further assertions check
+            that the results are also calculated correctly.
+        """
+        af = NestedNode.objects.create(name='Africa', lft=0, rght=10, parent=None)
+        ug = NestedNode.objects.create(name='Uganda', lft=1, rght=3, parent=af)
+        kla = NestedNode.objects.create(name='Kampala', lft=2,rght=2,parent=ug)
+        rw = NestedNode.objects.create(name='Rwanda', lft=4, rght=4, parent=af)
+        tz = NestedNode.objects.create(name='Tanzania', lft=5, rght=9, parent=af)
+        zb = NestedNode.objects.create(name='Zanzibar', lft=6, rght=8, parent=tz)
+        st = NestedNode.objects.create(name='Stone Town', lft=7, rght=7, parent=zb)
+        
+        eu = NestedNode.objects.create(name='Europe',lft=11, rght=13,parent=None)
+        es = NestedNode.objects.create(name='Spain',lft=12,rght=12,parent=eu)
+        
+        # The below query will fail with errors unless extra table aliasing is done
+        # properly
+        extra_query = list(NestedNode.objects\
+            .extra(tables=['queries_nestednode'],
+                   where=['T2.lft <= queries_nestednode.lft',
+                          'T2.rght >= queries_nestednode.rght',
+                          'T2.parent_id is NULL'])\
+            .extra(select={'continent_name':'T2.name'})\
+            .values('continent_name')\
+            .annotate(Count('pk')).extra(order_by=['continent_name']))
+        
+        # make sure the result is correct as well
+        self.assertEqual(extra_query[0]['continent_name'], 'Africa')
+        self.assertEqual(extra_query[0]['pk__count'], 7)
+
+        self.assertEqual(extra_query[1]['continent_name'], 'Europe')
+        self.assertEqual(extra_query[1]['pk__count'], 2)
