Changes between Initial Version and Version 3 of Ticket #2160


Ignore:
Timestamp:
Dec 17, 2006, 8:33:11 AM (18 years ago)
Author:
Russell Keith-Magee
Comment:

Original ticket description covered two problems. First problem was resubmitted as #3118, and has since been resolved. Ticket description has been revised to reflect the remaining problem.

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #2160

    • Property Component DocumentationDatabase wrapper
    • Property Owner changed from Jacob to Adrian Holovaty
    • Property Severity majornormal
    • Property Summary Documentation How Django knows to Update vs Insert not correctCan't use value of 0 for primary key
    • Property Priority highnormal
    • Property VersionSVN
  • Ticket #2160 – Description

    initial v3  
    1 The documentation at [http://www.djangoproject.com/documentation/db_api/#id1 here] says:
     1The DB API uses a 'bool(pk_val) == false' check to determine if a model instance is a new item needs to be INSERTed, or an existing entry that needs to be UPDATEd.
    22
    3 
    4 {{{
    5 - If the object's primary key attribute is set to a value that evaluates to False (such as None or the empty string), Django executes a SELECT query to determine whether a record with the given primary key already exists.
    6 }}}
    7 
    8 
    9 That is not true, since the code is that determines if it should check that the primary key exists is:
    10 
    11 
    12 {{{
    13 pk_val = self._get_pk_val()
    14 pk_set = bool(pk_val)
    15 record_exists = True
    16 if pk_set:
    17     # Determine whether a record with the primary key already exists.
    18     cursor.execute("SELECT 1 FROM %s WHERE %s=%%s LIMIT 1" % \
    19                 (backend.quote_name(self._meta.db_table), backend.quote_name(self._meta.pk.column)), [pk_val])
    20     # If it does already exist, do an UPDATE.
    21 }}}
    22 
    23 
    24 
    25 If a value that evaluates to "False" (as said in the documentation) is set to the pk the select is never executes and it just goes to the INSERT. Therefor the documentation should say:
    26 
    27 {{{
    28 - If the object's primary key attribute is NOT set to a value that evaluates to False (such as None or the empty string), Django executes a SELECT query to determine whether a record with the given primary key already exists.
    29 }}}
    30 
    31 And there should be a note about how to work around this for the people who uses a PK that evaluates to False such as 0, or Empty String.
     3Most databases use PK's starting at 1, but if you try to manually use a pk value of 0, the object cannot be saved - it can only be recreated.
Back to Top