Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #2160 -- Modified object save logic to check for ``pk is None``…

…, rather than ``bool(pk) == False``. This allows primary keys to take a value of 0. Thanks for the report, fgutierrez@aureal.com.pe.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4459 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 795de3a67ca7208e7628dffdf6723c81f71353e0 1 parent 695302c
Russell Keith-Magee authored February 04, 2007

Showing 1 changed file with 4 additions and 5 deletions. Show diff stats Hide diff stats

  1. 9  django/db/models/base.py
9  django/db/models/base.py
@@ -167,9 +167,8 @@ def save(self):
167 167
 
168 168
         # First, try an UPDATE. If that doesn't update anything, do an INSERT.
169 169
         pk_val = self._get_pk_val()
170  
-        pk_set = bool(pk_val)
171 170
         record_exists = True
172  
-        if pk_set:
  171
+        if pk_val is not None:
173 172
             # Determine whether a record with the primary key already exists.
174 173
             cursor.execute("SELECT 1 FROM %s WHERE %s=%%s LIMIT 1" % \
175 174
                 (backend.quote_name(self._meta.db_table), backend.quote_name(self._meta.pk.column)), [pk_val])
@@ -184,11 +183,11 @@ def save(self):
184 183
                         db_values + [pk_val])
185 184
             else:
186 185
                 record_exists = False
187  
-        if not pk_set or not record_exists:
  186
+        if pk_val is None or not record_exists:
188 187
             field_names = [backend.quote_name(f.column) for f in self._meta.fields if not isinstance(f, AutoField)]
189 188
             db_values = [f.get_db_prep_save(f.pre_save(self, True)) for f in self._meta.fields if not isinstance(f, AutoField)]
190 189
             # If the PK has been manually set, respect that.
191  
-            if pk_set:
  190
+            if pk_val is not None:
192 191
                 field_names += [f.column for f in self._meta.fields if isinstance(f, AutoField)]
193 192
                 db_values += [f.get_db_prep_save(f.pre_save(self, True)) for f in self._meta.fields if isinstance(f, AutoField)]
194 193
             placeholders = ['%s'] * len(field_names)
@@ -208,7 +207,7 @@ def save(self):
208 207
                     (backend.quote_name(self._meta.db_table),
209 208
                      backend.quote_name(self._meta.pk.column),
210 209
                      backend.get_pk_default_value()))
211  
-            if self._meta.has_auto_field and not pk_set:
  210
+            if self._meta.has_auto_field and pk_val is None:
212 211
                 setattr(self, self._meta.pk.attname, backend.get_last_insert_id(cursor, self._meta.db_table, self._meta.pk.column))
213 212
         transaction.commit_unless_managed()
214 213
 

0 notes on commit 795de3a

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