0

Prevoiusly my model consist of three records id (primary key), router_id, as_num and neighbor_id then I'm trying to alter 'id' field in database by changing a field name to from 'id' to 'auto_id'.

Here's my model

class dataFromFile(models.Model):
    auto_id = models.AutoField(primary_key=True, default=0)
    router_ip = models.CharField(max_length=20)
    as_num = models.IntegerField(default=0)
    neighbor_ip = models.CharField(max_length=20)

then using command python manage.py makemigrations peersite gave me a message "No changes detected in app 'peersite'" then using python manage.py sqlmigrate peersite 0001 seems fine with no error. But with python manage.py migrate peersite gave

 _mysql.connection.query(self, query)
django.db.utils.OperationalError: (1091, "Can't DROP 'id'; check that column/key exists")

I've checked in table dataFromFile in the database, also no 'id' field.

Any solution here ? Thank you in advance

Banthita Limwilai
  • 181
  • 1
  • 2
  • 10
  • This question https://stackoverflow.com/questions/2055784/what-is-the-best-approach-to-change-primary-keys-in-an-existing-django-app has your answer. – Bidhan Majhi Nov 19 '18 at 06:44
  • Did you previously hardcode the *id* field in your model or was it just the auto-created *id* field? – zypro Nov 19 '18 at 07:09

1 Answers1

2

If you are in development and don't have any valuable data in the database you can DROP DATABASE db_name; CREATE DATABASE db_name; Delete your migrations within your apps and run make migrations and migrate all over again. I know it's kind of a pain, but when I run into issues like this sometimes it saves time in the long run.