Reputation: 5800
I have to create a simple CRUD panel for an active MySQL database. When I try to migrate my application I receive the following error:
AssertionError: A model can't have more than one Autofield
I've read the following in The Django Book, Chapter 18:
Each generated model has an attribute for every field, including id primary key fields. However, recall that Django automatically adds an id primary key field if a model doesn’t have a primary key. Thus, you’ll want to remove any lines that look like this:
id = models.IntegerField(primary_key=True)
Not only are these lines redundant, but also they can cause problems if your application will be adding new records to these tables.
I have the same scenario with this field:
id_call = models.BigIntegerField(primary_key=True)
However, if I follow the above suggestion and remove this line, the original application (not the django application) using this table may not work properly because it could be calling data from this table using this id_call
field.
How can I resolve this situation?
Upvotes: 1
Views: 1736
Reputation: 1053
For me, by changing models.AutoField(unique=True)
to models.AutoField(primary_key=True)
, while working with a Wordpress database.
I had about 4 of them in generated models.py
by python manage.py --database='olddb' inspectdb > models.py
. Here i used one more db, say olddb
, if you use default db then you can remove --database='olddb'
.
I tried running python manage.py runserver
. So i fixed each line by things mentioned above.
Reference:-
https://docs.djangoproject.com/en/1.8/topics/db/models/#automatic-primary-key-fields
https://docs.djangoproject.com/en/1.8/topics/db/multi-db/#
Upvotes: 1