1

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?

MiniGunnR
  • 5,590
  • 8
  • 42
  • 66
  • Have you read [this](https://docs.djangoproject.com/en/1.7/howto/legacy-databases/#integrating-django-with-a-legacy-database)? – Leistungsabfall May 30 '15 at 11:28
  • Yes I have. That's how I've proceeded so far. But this doesn't give a solution for my problem. – MiniGunnR May 30 '15 at 11:33
  • 2
    See this [Automatic primary key fields](https://docs.djangoproject.com/en/1.8/topics/db/models/#automatic-primary-key-fields). I am using `uuid=models.CharField(max_length=22,primary_key=True,blank=True,editable=False)` as primary key instead of id. There's no id field in my MySQL table. – Ashish Gupta May 30 '15 at 11:50
  • show your models if you are not able to debug the problem. – Ashish Gupta May 30 '15 at 11:57
  • Thank you sir. The `primary_key=True` was given already when I used `inspectdb` to create the model. However, when I deleted and typed it again, it started working. – MiniGunnR May 30 '15 at 16:00
  • @MiniGunnR So include your answer and accept it. – Ajeeb.K.P Jul 20 '15 at 09:43

1 Answers1

1

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/#

Ajeeb.K.P
  • 1,013
  • 1
  • 13
  • 21