Opened 5 weeks ago
Last modified 2 weeks ago
#35957 new New feature
Allow AutoFields in composite primary keys
Reported by: | Csirmaz Bendegúz | Owned by: | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Simon Charette, Lily Foote | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
This is a follow up to #373 (CompositePrimaryKey), #8576 (Multiple AutoFields in a model), #27452 (SerialField).
At the moment, AutoField
s must set primary_key=True
.
This makes it impossible to use them with CompositePrimaryKey
.
Multiple AutoFields in a model (#8576) may not be useful, but having an AutoField
be part of a composite primary key has its uses. It's good practice to use surrogate keys, even if the table has a composite primary key (should this surrogate key be an auto-incremented field is another discussion).
So I think we should allow AutoField
s to not set primary_key=True
if they are part of a composite primary key.
Note: SQLite doesn't support AutoFields in composite primary keys.
Change History (5)
follow-up: 2 comment:1 by , 5 weeks ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 5 weeks ago
No, it's the opposite. The goal is to make AutoField
s work with composite primary keys. It's not Postgres-specific, this would work with all database backends (except SQLite).
The difference between #8576 and this is that this ticket won't allow multiple AutoField
s. A model can only have one AutoField
, and that AutoField
must be a primary key, or must be part of a composite primary key.
e.g.:
class Foo(models.Model): pk = models.CompositePrimaryKey("bar", "id", primary_key=True) bar = models.CharField(max_length=255) id = models.AutoField()
comment:3 by , 5 weeks ago
Cc: | added |
---|
Thank you Ben for clarifying. This seems to be outside what we discussed in the forum. Do you have more resources to point me to understand the bigger picture? I was assuming that our agreement and path forward with AutoField
s was described in the referenced forum post but before proceeding I would like to have the full context.
comment:4 by , 4 weeks ago
I think it aligns with what we discussed on the forum?
We keep the one AutoField per model restriction we currently have, but we'll allow AutoField
to be part of a composite primary key.
I don't think we should allow multiple AutoField
s, since only Postgres would support that and there's no strong use case for having multiple IDENTITY columns in a table. If consensus on this changes, then #8576 can be re-opened any time as it doesn't conflict with this ticket.
While the multiple AutoFields topic is contentious, everyone seems to agree that AutoField in composite primary keys should be added.
comment:5 by , 2 weeks ago
related forum topic:
https://forum.djangoproject.com/t/autofield-in-compositeprimarykey/37334
Hello Ben, thank for your creating this follow up ticket. I'm accepting this because of what was discussed and agreed in this Forum comment, but I do have a clarification question: in the Forum conversation, I understand it boiled down to two options:
If I understand correctly, this ticket would be solving item (2) above, so we would be providing a Postgresql-specific field, correct?