2

So, I committed and pushed all my code, and then deployed my web application successfully. Then, I added a new model to my 'home' app, which (for a reason I now understand, but doesn't matter here), created an IntegrityError (django.db.utils.IntegrityError: insert or update on table "foo" violates foreign key constraint "bar"). I ran python manage.py makemigrations, python manage.py migrate, which causes the the IntegrityError.

However, even if I remove all of my new model code(so that git status comes up with nothing), the IntegrityError still happens. If I connect to my db via a different python instance and download select * from django_migrations;, the latest db migration: 0020 there is eight migrations away from my latest local home/migrations migration file: 0028.

--> My question is: is it safe for me to delete my local 0021-0028 migration files? Will this fix my problem?

6
  • what version of django are you using? Also when exactly do you get this error? When you run migrations? Commented Sep 21, 2016 at 5:02
  • version 1.10. Sorry, should have been more clear about that. I got the error after running python manage.py makemigrations, python manage.py migrate Commented Sep 21, 2016 at 5:11
  • 1
    If your migrations wasn't applied it safe to delete them. Because they can be easily recreated with makemigrations. Commented Sep 21, 2016 at 5:13
  • Cool, thanks. I had tried running python manage.py makemigrations, python manage.py migrate both again, but error didn't go away. However, deleting the migrations and then doing that worked! Commented Sep 21, 2016 at 5:15
  • Don't forget to add this migrations to git repo. Commented Sep 21, 2016 at 5:15

2 Answers 2

2

If you haven't applied your migrations to db, it is safe to delete them and recreate them.

Possible reasons of why you run into this error are:

  1. You deleted your model code but, when you run migrate it reads your migration files (which has information about your deleted model) and tries to apply migration operations. If you didn't run makemigrations command after you've deleted your model, migration system won't be able to detect your changes and will think that your model is still there.
  2. Even if you've run makemigrations after you've deleted your model there'll be dependency issues in your migrations files, because the new migration files will depend on old ones (with which you had problems)

That's why we can say that it is safe to delete them, if they haven't applied, but at the same time you should be careful with your migration dependencies.

This documentation information maybe useful.

Sign up to request clarification or add additional context in comments.

Comments

0

OK, so I crossed my fingers, backed my local 0021-0028 migration files, and then deleted them. It worked. I think they key is that the migration files were not yet in the database yet, but not 100% sure. +1 if anyone can answer further for clarification.

1 Comment

yes, the migrations had not been applied for whatever reason

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.