Opened 16 years ago

Last modified 2 months ago

#8576 new

Multiple AutoFields in a model — at Initial Version

Reported by: honeyman Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords: multiple autofield
Cc: Csirmaz Bendegúz Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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 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 (1)

by honeyman, 16 years ago

Attachment: multiple_autofields.diff added

Multiple AutoField-s

Note: See TracTickets for help on using tickets.
Back to Top