Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.0.X] Fixed insert/update handling when no database interaction is …

…required.

Fixed #10205 as part of this.

Backport of r9926 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9927 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 016f12a2c27fd6006d43cc32af734dfd0ea96f98 1 parent 283c89e
Malcolm Tredinnick authored March 01, 2009
8  django/db/models/sql/query.py
@@ -1743,9 +1743,11 @@ def execute_sql(self, result_type=MULTI):
1743 1743
         iterator over the results if the result_type is MULTI.
1744 1744
 
1745 1745
         result_type is either MULTI (use fetchmany() to retrieve all rows),
1746  
-        SINGLE (only retrieve a single row), or None (no results expected, but
1747  
-        the cursor is returned, since it's used by subclasses such as
1748  
-        InsertQuery).
  1746
+        SINGLE (only retrieve a single row), or None. In this last case, the
  1747
+        cursor is returned if any query is executed, since it's used by
  1748
+        subclasses such as InsertQuery). It's possible, however, that no query
  1749
+        is needed, as the filters describe an empty set. In that case, None is
  1750
+        returned, to avoid any unnecessary database interaction.
1749 1751
         """
1750 1752
         try:
1751 1753
             sql, params = self.as_sql()
4  django/db/models/sql/subqueries.py
@@ -115,7 +115,7 @@ def execute_sql(self, result_type=None):
115 115
         tables, but their rowcounts are not returned).
116 116
         """
117 117
         cursor = super(UpdateQuery, self).execute_sql(result_type)
118  
-        rows = cursor.rowcount
  118
+        rows = cursor and cursor.rowcount or 0
119 119
         del cursor
120 120
         for query in self.get_related_updates():
121 121
             query.execute_sql(result_type)
@@ -307,7 +307,7 @@ def as_sql(self):
307 307
 
308 308
     def execute_sql(self, return_id=False):
309 309
         cursor = super(InsertQuery, self).execute_sql(None)
310  
-        if return_id:
  310
+        if return_id and cursor:
311 311
             return self.connection.ops.last_insert_id(cursor,
312 312
                     self.model._meta.db_table, self.model._meta.pk.column)
313 313
 
14  tests/regressiontests/queries/models.py
@@ -1056,6 +1056,20 @@ def __unicode__(self):
1056 1056
 Bug #9985 -- qs.values_list(...).values(...) combinations should work.
1057 1057
 >>> Note.objects.values_list("note", flat=True).values("id").order_by("id")
1058 1058
 [{'id': 1}, {'id': 2}, {'id': 3}]
  1059
+>>> Annotation.objects.filter(notes__in=Note.objects.filter(note="n1").values_list('note').values('id'))
  1060
+[<Annotation: a1>]
  1061
+
  1062
+Bug #10028 -- ordering by model related to nullable relations(!) should use
  1063
+outer joins, so that all results are included.
  1064
+>>> _ = Plaything.objects.create(name="p1")
  1065
+>>> Plaything.objects.all()
  1066
+[<Plaything: p1>]
  1067
+
  1068
+Bug #10205 -- When bailing out early because of an empty "__in" filter, we need
  1069
+to set things up correctly internally so that subqueries can continue properly.
  1070
+>>> Tag.objects.filter(name__in=()).update(name="foo")
  1071
+0
  1072
+
1059 1073
 """}
1060 1074
 
1061 1075
 # In Python 2.3 and the Python 2.6 beta releases, exceptions raised in __len__

0 notes on commit 016f12a

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