Rationale for unique AutoField's

I wonder if there is a reason why models can’t have more than one AutoField, and why AutoField’s must be a primary key. Looks like Django always behaved that way (see commit 4622e0ff82f82), but I didn’t find any rationale. In my mind, it should be possible to have multiple (non primary key) AutoField’s. Did I miss something obvious? Opinions?


See #8576 (Multiple AutoFields in a model) – Django

Not every database backend supports multiple auto fields in a table.

Sorry I missed that ticket.

Now the fact that not all backends support them is not a compelling argument for me. There are other features that are using a DatabaseFeatures attributes to have various behaviors depending on the backend.

I think it would be worth reconsidering. At least with MariaDB now one can create sequences and set up a second auto incrementing column that way: CREATE SEQUENCE - MariaDB Knowledge Base

I think creating a PoC patch would help make a case for the feature.

Unforced AutoFields to be primary keys by claudep · Pull Request #17169 · django/django · GitHub could be a start. I plan to add support/test for two autofield’s tables when the current PR passes tests.

Do we really want to keep the checks for some databases (based on database features) or just let things happen and let database complains for unsupported table layouts?

Not completely finished, but my PR referenced above is suitable as a PoC. Comments welcome!

I just want to chime in and say I could use this feature. :blush: Currently if you want multiple auto fields backed by the database you need to setup less-than-ideal workarounds.

Thanks for sharing your support.

What do you suggest now to push this forward?

We bug Simon, Adam, Felix & Nessita :rofl: