Django 1.8, syncdb not working, throwing a foreign key constraint error

1k Views Asked by At

Since I upgrade to Django 1.8 from 1.7, I have got this foreign key constraint error.

File "c:project\env\lib\site-packages\mysql_python-1.2.5-py2.7-win32.egg/MySQLdb\connections.py line 36, in defaulterrorhandler raise errorclass, errorvalue, 

Django.db.utils.IntergrityError: 'Cannot add foreing key contraint

What's some wrong with django 1.8 (latest version)?

2

There are 2 best solutions below

0
Wagh On BEST ANSWER

Try this

DATABASES = {
'default': {
    ...         
    'OPTIONS': {
         "init_command": "SET foreign_key_checks = 0;",
    },
    'STORAGE_ENGINE': 'MyISAM / INNODB / ETC'
 }
}
1
Rachel On

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