Multiple AutoFields in a model
|Reported by:||honeyman||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||master|
|Cc:||Triage Stage:||Design decision needed|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Description (last modified by ramiro)
Got the assertions failed recently:
File "C:\Projects\houserules\www\hrdb\events\hrpercustomerobject.py", line 14, in <module> class HRPerCustomerObject(models.Model): File "C:\Projects\houserules\www\hrdb\events\hrpercustomerobject.py", line 37, in HRPerCustomerObject id_percustomer = models.AutoField ( 'Per-customer ID' ) File "C:\Projects\houserules\www\django\db\models\fields\__init__.py", line 425, in __init__ assert kwargs.get('primary_key', False) is True, "%ss must have primary_key=True." % self.__class__.__name__ AssertionError: AutoFields must have primary_key=True. make: *** [_reset] Error 1
File "C:\Projects\houserules\www\django\db\models\options.py", line 117, in _prepare model.add_to_class('id', auto) File "C:\Projects\houserules\www\django\db\models\base.py", line 139, in add_to_class value.contribute_to_class(cls, name) File "C:\Projects\houserules\www\django\db\models\fields\__init__.py", line 459, in contribute_to_class assert not cls._meta.has_auto_field, "A model can't have more than one AutoField." AssertionError: A model can't have more than one AutoField. make: *** [_reset] Error 1
Depending on the logic and the scenario, it may be really worthy to have several AutoFields in a model (for example, when one or both of them in some cases is controlled manually), one of which will definitely not be a primary key.
I roughly described one of possible scenarios in IRC, though this may be not the only one.
Please note that the databases itself do not imply such behaviour (auto_increment field in MySQL does not imply a primary key, neither SERIAL field in PostgreSQL does), so it is unsafe and overrestrictive to add such a limitation in Django.
The existing documentation on AutoField also does not imply it should be used for primary keys only.
My proposal is to withdraw such a limitation; seems that it is only required to remove two assert lines from the code�(well, and the related tests, of course).
Change History (9)
Changed 8 years ago by honeyman
comment:1 Changed 8 years ago by ramiro
- Description modified (diff)
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
comment:2 Changed 8 years ago by ElliottM
- Keywords multiple autofield added
- Triage Stage changed from Unreviewed to Accepted
comment:4 in reply to: ↑ 3 Changed 8 years ago by kmtracey
- Triage Stage changed from Accepted to Design decision needed
comment:5 Changed 7 years ago by mtredinnick
- Resolution set to wontfix
- Status changed from new to closed
comment:6 Changed 7 years ago by seanmonstar
- Has patch unset
- Resolution wontfix deleted
- Status changed from closed to reopened
comment:7 Changed 7 years ago by kmtracey
- Resolution set to wontfix
- Status changed from reopened to closed