Permalink
Browse files

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

  • Loading branch information...
1 parent 42dc3b9 commit 19d6bc7acd68f5e93e1ffe9f45b8e8b9021c2151 @malcolmt malcolmt committed Apr 23, 2008
Showing with 15 additions and 4 deletions.
  1. +9 −4 django/db/models/sql/query.py
  2. +6 −0 tests/regressiontests/queries/models.py
View
13 django/db/models/sql/query.py
@@ -425,7 +425,12 @@ def get_from_clause(self):
for alias in self.tables:
if not self.alias_refcount[alias]:
continue
- name, alias, join_type, lhs, lhs_col, col, nullable = self.alias_map[alias]
+ try:
+ name, alias, join_type, lhs, lhs_col, col, nullable = self.alias_map[alias]
+ except KeyError:
+ # Extra tables can end up in self.tables, but not in the
+ # alias_map if they aren't in a join. That's OK. We skip them.
+ continue
alias_str = (alias != name and ' %s' % alias or '')
if join_type and not first:
result.append('%s %s%s ON (%s.%s = %s.%s)'
@@ -436,10 +441,10 @@ def get_from_clause(self):
result.append('%s%s%s' % (connector, qn(name), alias_str))
first = False
for t in self.extra_tables:
- alias, created = self.table_alias(t)
- if created:
+ alias, unused = self.table_alias(t)
+ if alias not in self.alias_map:
connector = not first and ', ' or ''
- result.append('%s%s' % (connector, alias))
+ result.append('%s%s' % (connector, qn(alias)))
first = False
return result, []
View
6 tests/regressiontests/queries/models.py
@@ -634,5 +634,11 @@ class Meta:
Traceback (most recent call last):
...
IndexError: ...
+
+Bug #7045 -- extra tables used to crash SQL construction on the second use.
+>>> qs = Ranking.objects.extra(tables=['django_site'])
+>>> s = qs.query.as_sql()
+>>> s = qs.query.as_sql() # test passes if this doesn't raise an exception.
+
"""}

0 comments on commit 19d6bc7

Please sign in to comment.