0

I'm fairly new to Django, I am using 1.10 version. I reached a point where I was unable to migrate because of an error, and so backed up to an earlier migration using ./manage.py migrate myapp 0003_auto_20160426_2022 and deleted the later migration files. I then repaired my models.py and ran makemigrations, which worked fine. But when I attempted to migrate, I received the following error (only showing last few lines)

File "/Users/wahhab/Sites/rts/env/lib/python3.5/site-packages/MySQLdb/connections.py", line 280, in query _mysql.connection.query(self, query) django.db.utils.OperationalError: (1022, "Can't write; duplicate key in table '#sql-72_4a6'")

I don't know how to move forward from this point so that I can continue working on my project. I have data in other apps but only a little test data in this new app so far, so I have considered deleting all migrations and the MySQL tables for this app and starting over, but I don't want to create a worse mess than I have and don't know what is causing this error. Any advice is welcome. Thanks!

Saeid
  • 4,147
  • 7
  • 27
  • 43
Wahhabb
  • 19
  • 3
  • The issue with migrations is that although they have a great philosophy around them, it is very difficult to make ends meet in some tiny details that only a very experienced developer can tackle with. [My hackish approach](http://stackoverflow.com/a/27408592/2996101) is to always destroy the development database, rebuild it from backup and then ```makemigrations```. I have a handy script for that and I just wait for it to finish. **Hackish** but it keeps my mental condition safer. – raratiru Sep 09 '16 at 17:44

1 Answers1

0

Okay so a hackish solution has already been suggested by @raratiru.

Now the explanation for WHY you were facing the above problem is that when you deleted you migrations, Django resets all its counters, which also includes the counter of the key, but because the deletion of migrations was a manual one, the data still persists in your DB.

So there already exists objects in your DB with key = 1, 2, 3 ....and so on. But Django doesn't know this and hence when you have deleted just the migrations and not the data, Django faces a clash of key as Django again starts assigning automatic key values from 1, which already exists in the DB. But as the key needs to be unique, it gives you an error.

Hence if you delete both the migrations and the data, you don't get thee error.

Ankush Raghuvanshi
  • 1,392
  • 11
  • 17
  • Thank you for the explanation. Going to my database, I see that there are some tables created by Django. I tried deleting just the migration entries associated with the migration, but that didn't work. So I've backed up the tables from my other apps, and will try next deleting all the tables, deleting all migration files except the initial migrations, and rebuilding everything. I'll let you know if that works. – Wahhabb Sep 11 '16 at 04:58
  • I succeeded in solving the problem without deleting the database. I had missed the fact that authorizations were created for the tables I was deleting; that caused problems when migrate attempted to re-add the tables and therefore re-add the authorizations for them. – Wahhabb Sep 12 '16 at 18:12
  • 1
    For the benefit of anyone else with this problem, here are the basic steps I took: 1. Delete migrations 2. Delete tables from MySQL 3. Delete entries from table django_migrations 4. Delete entries from django_content_type 5. Delete entries from auth_permission 6. Rerun makemigrations and migrate. (Hope I got everything here; I did a lot of unnecessary back-and-forth as well). Thanks to Ankush and @raratiru for their help. – Wahhabb Sep 12 '16 at 18:19