Opened 6 years ago

Closed 4 years ago

#17713 closed Cleanup/optimization (fixed)

allows_primary_key_0 is misnamed

Reported by: Claude Paroz Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: anssi.kaariainen@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


In django/db/backends/

    # Can an object have a primary key of 0? MySQL says No.
    allows_primary_key_0 = True

This is not totally right. MySQL refuses 0 only if the primary key is an autoincrement key. See also comments in #17653. At the very least, the comment should be updated. And ideally, allows_primary_key_0 should be renamed as (for example) allows_auto_pk_0.

Change History (6)

comment:1 Changed 6 years ago by Anssi Kääriäinen

Cc: anssi.kaariainen@… added
Triage Stage: UnreviewedAccepted

This is a really minor issue, but also an easy one to fix.

If somebody with older MySQL versions could test this it would be valuable information. I have tested this on 5.1 where id INTEGER PRIMARY KEY accepts zero as a value. Based on that marking as accepted.

I think changing the name is the correct thing to do.

comment:2 Changed 5 years ago by Vajrasky Kok


Later, in a separate ticket I will create a unit test for exercising the MySQL capability of using non-autoincrement primary key with value 0.

comment:3 Changed 4 years ago by Tim Graham

Has patch: set

I guess the only concern here is whether or not setting this attribute on your own database backend is considered private API subject to change?

comment:4 Changed 4 years ago by Vajrasky Kok

This is the separate ticket for adding unit test exercising the capability of having zero non-autoincrement primary key, #21517.

comment:5 Changed 4 years ago by Vajrasky Kok

@timo: or we can add another attribute (allows_auto_pk_0) and keep this one (allows_primary_key_0).

comment:6 Changed 4 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: newclosed

In d3cf6cfacfb828faad4f4f97c904e259304649b3:

Fixed #17713 -- Renamed BaseDatabaseFeatures.allows_primary_key_0 to allows_auto_pk_0.

MySQL does allow primary key with value 0. It only forbids autoincrement
primary key with value 0.

Thanks Claude Paroz for the report.

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