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