Django: “Cannot add foreign key constraint”

Upgraded to Django 1.8 and now you get this error when you run your tests?

Have you created migrations for all your apps? If not, you may well be hitting the problem that the database tables are being created in the wrong order, which will give you this error.

If you have an existing Django 1.7 or earlier project, then you need to create the initial migration files, and then fake the initial migration, as described here

https://docs.djangoproject.com/en/1.8/topics/migrations/#adding-migrations-to-apps

Create the migration with

$ python manage.py make migrations your_app_label

And then fake the application

$ python manage.py migrate –fake-initial your_app_label

One thought on “Django: “Cannot add foreign key constraint””

Leave a Reply

Your email address will not be published. Required fields are marked *