﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
8576	Multiple AutoFields in a model	honeyman	nobody	"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).
"	Uncategorized	closed	Database layer (models, ORM)	dev	Normal	wontfix	multiple autofield	Csirmaz Bendegúz	Design decision needed	0	0	0	0	0	0
