Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

queryset-refactor: Fixed a crash when using extra(tables=...). Fixed …

…#7045.

git-svn-id: http://code.djangoproject.com/svn/django/branches/queryset-refactor@7447 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 19d6bc7acd68f5e93e1ffe9f45b8e8b9021c2151 1 parent 42dc3b9
Malcolm Tredinnick authored April 23, 2008
13  django/db/models/sql/query.py
@@ -425,7 +425,12 @@ def get_from_clause(self):
425 425
         for alias in self.tables:
426 426
             if not self.alias_refcount[alias]:
427 427
                 continue
428  
-            name, alias, join_type, lhs, lhs_col, col, nullable = self.alias_map[alias]
  428
+            try:
  429
+                name, alias, join_type, lhs, lhs_col, col, nullable = self.alias_map[alias]
  430
+            except KeyError:
  431
+                # Extra tables can end up in self.tables, but not in the
  432
+                # alias_map if they aren't in a join. That's OK. We skip them.
  433
+                continue
429 434
             alias_str = (alias != name and ' %s' % alias or '')
430 435
             if join_type and not first:
431 436
                 result.append('%s %s%s ON (%s.%s = %s.%s)'
@@ -436,10 +441,10 @@ def get_from_clause(self):
436 441
                 result.append('%s%s%s' % (connector, qn(name), alias_str))
437 442
             first = False
438 443
         for t in self.extra_tables:
439  
-            alias, created = self.table_alias(t)
440  
-            if created:
  444
+            alias, unused = self.table_alias(t)
  445
+            if alias not in self.alias_map:
441 446
                 connector = not first and ', ' or ''
442  
-                result.append('%s%s' % (connector, alias))
  447
+                result.append('%s%s' % (connector, qn(alias)))
443 448
                 first = False
444 449
         return result, []
445 450
 
6  tests/regressiontests/queries/models.py
@@ -634,5 +634,11 @@ class Meta:
634 634
 Traceback (most recent call last):
635 635
 ...
636 636
 IndexError: ...
  637
+
  638
+Bug #7045 -- extra tables used to crash SQL construction on the second use.
  639
+>>> qs = Ranking.objects.extra(tables=['django_site'])
  640
+>>> s = qs.query.as_sql()
  641
+>>> s = qs.query.as_sql()   # test passes if this doesn't raise an exception.
  642
+
637 643
 """}
638 644
 

0 notes on commit 19d6bc7

Please sign in to comment.
Something went wrong with that request. Please try again.