Django – A model can't have more than one AutoField


This problem randomly started appearing for me. I know that Django generates its own ids but a lot my code has been using custom AutoFields which HAS been working. I added a new class today and tried to makemigrations but this error keeps showing up.

I DID remove all instances of AutoFields and retried migrating but the problem still persists which leads me to believe it’s something else… I don’t believe my Django version has changed at all…


    Traceback (most recent call last):
  File "", line 10, in <module>
  File "C:\Python34\lib\site-packages\django\core\management\", line
338, in execute_from_command_line
  File "C:\Python34\lib\site-packages\django\core\management\", line
330, in execute
  File "C:\Python34\lib\site-packages\django\core\management\", line 390,
 in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Python34\lib\site-packages\django\core\management\", line 441,
 in execute
    output = self.handle(*args, **options)
  File "C:\Python34\lib\site-packages\django\core\management\commands\makemigrat", line 98, in handle
  File "C:\Python34\lib\site-packages\django\db\migrations\", line 326,
 in project_state
    return self.graph.make_state(nodes=nodes, at_end=at_end, real_apps=list(self
  File "C:\Python34\lib\site-packages\django\db\migrations\", line 231,
in make_state
    project_state = self.nodes[node].mutate_state(project_state, preserve=False)

  File "C:\Python34\lib\site-packages\django\db\migrations\", line 8
3, in mutate_state
    operation.state_forwards(self.app_label, new_state)
  File "C:\Python34\lib\site-packages\django\db\migrations\operations\"
, line 51, in state_forwards
    state.reload_model(app_label, self.model_name_lower)
  File "C:\Python34\lib\site-packages\django\db\migrations\", line 152,
in reload_model
  File "C:\Python34\lib\site-packages\django\db\migrations\", line 262,
in render_multiple
  File "C:\Python34\lib\site-packages\django\db\migrations\", line 546,
in render
  File "C:\Python34\lib\site-packages\django\db\models\", line 189, in __
    new_class.add_to_class(obj_name, obj)
  File "C:\Python34\lib\site-packages\django\db\models\", line 324, in ad
    value.contribute_to_class(cls, name)
  File "C:\Python34\lib\site-packages\django\db\models\fields\", line
 989, in contribute_to_class
    "A model can't have more than one AutoField."
AssertionError: A model can't have more than one AutoField.

Here’s an example of one of my fields:

assetid = models.AutoField(primary_key=True)

A lot of my code already depends on the name itself so changing it is going to be a big issue. Furthermore, this was working perfectly before! I just can’t seem to migrate it now. I should mention im using a sqlite3 db.


Deleted migration files and most recent history and fixed it.

Answered By – Kevin Pei

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published