Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #8039 -- Make sure that extra(tables=...) tables are always inc…

…luded in

the resulting SQL. Previously, an optimisation was removing them in some corner
cases.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@8429 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 556fbc78a636d2be5c66a4393361476a6de091bc 1 parent b25d759
Malcolm Tredinnick malcolmt authored
5 django/db/models/sql/query.py
View
@@ -542,7 +542,10 @@ def get_from_clause(self):
first = False
for t in self.extra_tables:
alias, unused = self.table_alias(t)
- if alias not in self.alias_map:
+ # 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.alias_map or self.alias_refcount[alias] == 1:
connector = not first and ', ' or ''
result.append('%s%s' % (connector, qn(alias)))
first = False
11 tests/regressiontests/extra_regress/models.py
View
@@ -24,6 +24,10 @@ def new_revision(self):
new_revision.pk = None
return new_revision
+class Order(models.Model):
+ created_by = models.ForeignKey(User)
+ text = models.TextField()
+
__test__ = {"API_TESTS": """
# Regression tests for #7314 and #7372
@@ -87,4 +91,11 @@ def new_revision(self):
>>> qs[:1]
[<User: fred>]
+# Regression test for #8039: Ordering sometimes removed relevant tables from
+# extra(). This test is the critical case: ordering uses a table, but then
+# removes the reference because of an optimisation. The table should still be
+# present because of the extra() call.
+>>> Order.objects.extra(where=["username=%s"], params=["fred"], tables=["auth_user"]).order_by('created_by')
+[]
+
"""}
Please sign in to comment.
Something went wrong with that request. Please try again.