Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 authored
5  django/db/models/sql/query.py
@@ -542,7 +542,10 @@ def get_from_clause(self):
542 542
             first = False
543 543
         for t in self.extra_tables:
544 544
             alias, unused = self.table_alias(t)
545  
-            if alias not in self.alias_map:
  545
+            # Only add the alias if it's not already present (the table_alias()
  546
+            # calls increments the refcount, so an alias refcount of one means
  547
+            # this is the only reference.
  548
+            if alias not in self.alias_map or self.alias_refcount[alias] == 1:
546 549
                 connector = not first and ', ' or ''
547 550
                 result.append('%s%s' % (connector, qn(alias)))
548 551
                 first = False
11  tests/regressiontests/extra_regress/models.py
@@ -24,6 +24,10 @@ def new_revision(self):
24 24
         new_revision.pk = None
25 25
         return new_revision
26 26
 
  27
+class Order(models.Model):
  28
+    created_by = models.ForeignKey(User)
  29
+    text = models.TextField()
  30
+
27 31
 __test__ = {"API_TESTS": """
28 32
 # Regression tests for #7314 and #7372
29 33
 
@@ -87,4 +91,11 @@ def new_revision(self):
87 91
 >>> qs[:1]
88 92
 [<User: fred>]
89 93
 
  94
+# Regression test for #8039: Ordering sometimes removed relevant tables from
  95
+# extra(). This test is the critical case: ordering uses a table, but then
  96
+# removes the reference because of an optimisation. The table should still be
  97
+# present because of the extra() call.
  98
+>>> Order.objects.extra(where=["username=%s"], params=["fred"], tables=["auth_user"]).order_by('created_by')
  99
+[]
  100
+
90 101
 """}

0 notes on commit 556fbc7

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