Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed insert/update handling when no database interaction is required.

Fixed #10205 as part of this.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@9926 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c6a404d1e977cae75345594bf264ca103e1a1d33 1 parent bbea457
Malcolm Tredinnick authored March 01, 2009
8  django/db/models/sql/query.py
@@ -2012,9 +2012,11 @@ def execute_sql(self, result_type=MULTI):
2012 2012
         iterator over the results if the result_type is MULTI.
2013 2013
 
2014 2014
         result_type is either MULTI (use fetchmany() to retrieve all rows),
2015  
-        SINGLE (only retrieve a single row), or None (no results expected, but
2016  
-        the cursor is returned, since it's used by subclasses such as
2017  
-        InsertQuery).
  2015
+        SINGLE (only retrieve a single row), or None. In this last case, the
  2016
+        cursor is returned if any query is executed, since it's used by
  2017
+        subclasses such as InsertQuery). It's possible, however, that no query
  2018
+        is needed, as the filters describe an empty set. In that case, None is
  2019
+        returned, to avoid any unnecessary database interaction.
2018 2020
         """
2019 2021
         try:
2020 2022
             sql, params = self.as_sql()
4  django/db/models/sql/subqueries.py
@@ -117,7 +117,7 @@ def execute_sql(self, result_type=None):
117 117
         tables, but their rowcounts are not returned).
118 118
         """
119 119
         cursor = super(UpdateQuery, self).execute_sql(result_type)
120  
-        rows = cursor.rowcount
  120
+        rows = cursor and cursor.rowcount or 0
121 121
         del cursor
122 122
         for query in self.get_related_updates():
123 123
             query.execute_sql(result_type)
@@ -315,7 +315,7 @@ def as_sql(self):
315 315
 
316 316
     def execute_sql(self, return_id=False):
317 317
         cursor = super(InsertQuery, self).execute_sql(None)
318  
-        if return_id:
  318
+        if return_id and cursor:
319 319
             return self.connection.ops.last_insert_id(cursor,
320 320
                     self.model._meta.db_table, self.model._meta.pk.column)
321 321
 
6  tests/regressiontests/queries/models.py
@@ -1081,6 +1081,12 @@ def __unicode__(self):
1081 1081
 >>> _ = Plaything.objects.create(name="p1")
1082 1082
 >>> Plaything.objects.all()
1083 1083
 [<Plaything: p1>]
  1084
+
  1085
+Bug #10205 -- When bailing out early because of an empty "__in" filter, we need
  1086
+to set things up correctly internally so that subqueries can continue properly.
  1087
+>>> Tag.objects.filter(name__in=()).update(name="foo")
  1088
+0
  1089
+
1084 1090
 """}
1085 1091
 
1086 1092
 # In Python 2.3 and the Python 2.6 beta releases, exceptions raised in __len__

0 notes on commit c6a404d

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