Opened 3 years ago

Closed 18 months ago

#17713 closed Cleanup/optimization (fixed)

allows_primary_key_0 is misnamed

Reported by: claudep 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

Description

In django/db/backends/__init__.py:

    # 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 3 years ago by akaariai

  • Cc anssi.kaariainen@… added
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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 21 months ago by vajrasky

PR: https://github.com/django/django/pull/1984

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 21 months ago by timo

  • 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 21 months ago by vajrasky

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

comment:5 Changed 21 months ago by vajrasky

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

comment:6 Changed 18 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from new to closed

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