Opened 18 years ago

Last modified 16 years ago

#2160 closed defect

Documentation How Django knows to Update vs Insert not correct — at Initial Version

Reported by: fgutierrez AT aureal.com.pe Owned by: Jacob
Component: Database layer (models, ORM) Version: dev
Severity: normal Keywords:
Cc: Triage Stage: Design decision needed
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

The documentation at here says:

- 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.

That is not true, since the code is that determines if it should check that the primary key exists is:

pk_val = self._get_pk_val()
pk_set = bool(pk_val)
record_exists = True
if pk_set:
    # Determine whether a record with the primary key already exists.
    cursor.execute("SELECT 1 FROM %s WHERE %s=%%s LIMIT 1" % \
                (backend.quote_name(self._meta.db_table), backend.quote_name(self._meta.pk.column)), [pk_val])
    # If it does already exist, do an UPDATE.

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:

- 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.

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.

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top