Django "Cannot add or update a child row: a foreign key constraint fails" Django "Cannot add or update a child row: a foreign key constraint fails" django django

Django "Cannot add or update a child row: a foreign key constraint fails"


Some of my tables were in InnoDB and some were in MyISAM... I changed everything to MyISAM and the problem was solved.


DATABASES = {'default': {    ...             'OPTIONS': {         "init_command": "SET foreign_key_checks = 0;",    }, }}

(According to the official doc)In previous versions of Django, fixtures with forward references (i.e. relations to rows that have not yet been inserted into the database) would fail to load when using the InnoDB storage engine. This was due to the fact that InnoDB deviates from the SQL standard by checking foreign key constraints immediately instead of deferring the check until the transaction is committed. This problem has been resolved in Django 1.4.


To avoid this happening what you can also do is set your STORAGE_ENGINE in your settings.py

for django >= 1.2

DATABASES = {    'default': {        ...        'STORAGE_ENGINE': 'MyISAM / INNODB / ETC'    }}

for django <= 1.2

DATABASE_STORAGE_ENGINE = "MyISAM / INNODB / ETC"

Please note this is only valid for MySQL