home / django_tickets / tickets

tickets: 8576

This data as json

id created changetime last_pulled_from_trac stage status component type severity version resolution summary description owner reporter keywords easy has_patch needs_better_patch needs_tests needs_docs ui_ux
8576 2008-08-26 22:30:47 2021-11-23 15:54:31 2022-03-06 03:42:37.222924 Design decision needed closed Database layer (models, ORM) Uncategorized Normal dev wontfix Multiple AutoFields in a model 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 }}} and {{{ 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 [http://oebfare.com/logger/django/2008/08/26/ 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). nobody honeyman multiple autofield 0 0 0 0 0 0
Powered by Datasette · Queries took 2.717ms