Opened 12 hours ago
Last modified 96 minutes ago
#36075 assigned Cleanup/optimization
Field.primary_key documentation should details its interaction with CompositePrimaryKey
Reported by: | Simon Charette | Owned by: | Simon Charette |
---|---|---|---|
Component: | Documentation | Version: | dev |
Severity: | Release blocker | Keywords: | |
Cc: | Csirmaz Bendegúz | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
A particular focus should be put on the fact that the flag will and should only be set to True
if the field is the single member of the primary key. When a composite primary is defined on the model this flag will be False
for all the fields defined on the model and _meta.pk_fields
(which should be documented) should be used instead to build composite primary key ready third-party application.
In other words, it should be made clear that code that relies solely on Field.primary_key
is not composite primary key ready and that _meta.pk_fields
should be used instead. I feel like this is something that should be clearly pointed out in the composite primary key documentation on how to make reusable app code composite primary key ready.
Change History (6)
comment:1 by , 12 hours ago
Description: | modified (diff) |
---|
comment:2 by , 12 hours ago
Description: | modified (diff) |
---|
comment:3 by , 12 hours ago
Description: | modified (diff) |
---|
comment:4 by , 11 hours ago
Severity: | Normal → Release blocker |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:6 by , 96 minutes ago
Owner: | set to |
---|---|
Status: | new → assigned |
Sleeping on it I think it's fine that we keep things as they are code wise on the assignment of primary_key = False
when used with CompositePrimaryKey
as long as we clearly document that _meta.pk_fields
should be used. The reason is that many of the field.primary_key
usages immediately stop when they first their first match so they are likely broken anyway.
Forcing third-party applications that wish to support composite primary keys to migrate to _meta.pk_fields
and abandon .primary_key
seem like a better option that having code implicitly break because they only expect one field to have primary_key = True
which is decade old assumption.
From #36074
I am not sure how best to reword the title but I think this piece of work (checking the existing checks against the
primary_key
attribute and whether they should be using field in opts.pk_fields, as well as checking the decision to not setting this flag to True when included as a member of CompositePrimaryKey) could be part of this ticket, as this documentation should accompany those changes.