Author: mtredinnick
Date: 2009-02-28 19:56:59 -0600 (Sat, 28 Feb 2009)
New Revision: 9926

Modified:
   django/trunk/django/db/models/sql/query.py
   django/trunk/django/db/models/sql/subqueries.py
   django/trunk/tests/regressiontests/queries/models.py
Log:
Fixed insert/update handling when no database interaction is required.

Fixed #10205 as part of this.

Modified: django/trunk/django/db/models/sql/query.py
===================================================================
--- django/trunk/django/db/models/sql/query.py  2009-03-01 01:35:36 UTC (rev 
9925)
+++ django/trunk/django/db/models/sql/query.py  2009-03-01 01:56:59 UTC (rev 
9926)
@@ -2012,9 +2012,11 @@
         iterator over the results if the result_type is MULTI.
 
         result_type is either MULTI (use fetchmany() to retrieve all rows),
-        SINGLE (only retrieve a single row), or None (no results expected, but
-        the cursor is returned, since it's used by subclasses such as
-        InsertQuery).
+        SINGLE (only retrieve a single row), or None. In this last case, the
+        cursor is returned if any query is executed, since it's used by
+        subclasses such as InsertQuery). It's possible, however, that no query
+        is needed, as the filters describe an empty set. In that case, None is
+        returned, to avoid any unnecessary database interaction.
         """
         try:
             sql, params = self.as_sql()

Modified: django/trunk/django/db/models/sql/subqueries.py
===================================================================
--- django/trunk/django/db/models/sql/subqueries.py     2009-03-01 01:35:36 UTC 
(rev 9925)
+++ django/trunk/django/db/models/sql/subqueries.py     2009-03-01 01:56:59 UTC 
(rev 9926)
@@ -117,7 +117,7 @@
         tables, but their rowcounts are not returned).
         """
         cursor = super(UpdateQuery, self).execute_sql(result_type)
-        rows = cursor.rowcount
+        rows = cursor and cursor.rowcount or 0
         del cursor
         for query in self.get_related_updates():
             query.execute_sql(result_type)
@@ -315,7 +315,7 @@
 
     def execute_sql(self, return_id=False):
         cursor = super(InsertQuery, self).execute_sql(None)
-        if return_id:
+        if return_id and cursor:
             return self.connection.ops.last_insert_id(cursor,
                     self.model._meta.db_table, self.model._meta.pk.column)
 

Modified: django/trunk/tests/regressiontests/queries/models.py
===================================================================
--- django/trunk/tests/regressiontests/queries/models.py        2009-03-01 
01:35:36 UTC (rev 9925)
+++ django/trunk/tests/regressiontests/queries/models.py        2009-03-01 
01:56:59 UTC (rev 9926)
@@ -1081,6 +1081,12 @@
 >>> _ = Plaything.objects.create(name="p1")
 >>> Plaything.objects.all()
 [<Plaything: p1>]
+
+Bug #10205 -- When bailing out early because of an empty "__in" filter, we need
+to set things up correctly internally so that subqueries can continue properly.
+>>> Tag.objects.filter(name__in=()).update(name="foo")
+0
+
 """}
 
 # In Python 2.3 and the Python 2.6 beta releases, exceptions raised in __len__


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to