Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

Error:

    Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line
338, in execute_from_command_line
    utility.execute()
  File "C:\Python34\lib\site-packages\django\core\management\__init__.py", line
330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python34\lib\site-packages\django\core\management\base.py", line 390,
 in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Python34\lib\site-packages\django\core\management\base.py", line 441,
 in execute
    output = self.handle(*args, **options)
  File "C:\Python34\lib\site-packages\django\core\management\commands\makemigrat
ions.py", line 98, in handle
    loader.project_state(),
  File "C:\Python34\lib\site-packages\django\db\migrations\loader.py", line 326,
 in project_state
    return self.graph.make_state(nodes=nodes, at_end=at_end, real_apps=list(self
.unmigrated_apps))
  File "C:\Python34\lib\site-packages\django\db\migrations\graph.py", 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\migration.py", line 8
3, in mutate_state
    operation.state_forwards(self.app_label, new_state)
  File "C:\Python34\lib\site-packages\django\db\migrations\operations\fields.py"
, line 51, in state_forwards
    state.reload_model(app_label, self.model_name_lower)
  File "C:\Python34\lib\site-packages\django\db\migrations\state.py", line 152,
in reload_model
    self.apps.render_multiple(states_to_be_rendered)
  File "C:\Python34\lib\site-packages\django\db\migrations\state.py", line 262,
in render_multiple
    model.render(self)
  File "C:\Python34\lib\site-packages\django\db\migrations\state.py", line 546,
in render
    body,
  File "C:\Python34\lib\site-packages\django\db\models\base.py", line 189, in __
new__
    new_class.add_to_class(obj_name, obj)
  File "C:\Python34\lib\site-packages\django\db\models\base.py", line 324, in ad
d_to_class
    value.contribute_to_class(cls, name)
  File "C:\Python34\lib\site-packages\django\db\models\fields\__init__.py", 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.

like image 650
Kevin Pei Avatar asked Jul 24 '15 01:07

Kevin Pei


1 Answers

This is Because Django By default uses AutoField for the id.....so, if you want other fields to be AutoField, then make sure you confirm primary_key=True. Doing so deletes the id field from the Database.

I have used Mysql as my database. Be sure this problem might not solve in other databases.

like image 112
Rajesh Pudasaini Avatar answered Sep 18 '22 02:09

Rajesh Pudasaini