72

I have a model Coupon, and a model Photo with a ForeignKey to it:

class Photo(models.Model):
    coupon = models.ForeignKey(Coupon,
                               related_name='description_photos')
    title = models.CharField(max_length=100)
    image = models.ImageField(upload_to='images')

I set up inlines in the admin so now I'm able to add photos to a coupon from the admin.

I attempt to add one, and upload is successful, but then I get Django's debug page with this error:

IntegrityError at /admin/coupon/coupon/321/
(1452, 'Cannot add or update a child row: a foreign key constraint fails (`my_project`.`coupon_photo`, CONSTRAINT `coupon_id_refs_id_90d7f06` FOREIGN KEY (`coupon_id`) REFERENCES `coupon_coupon` (`id`))')

What is this and how can I solve this problem?

(If it matters, this is a MySQL database.)

EDIT: I tried it on an Sqlite3 database that has a slightly different dataset, and it worked, so perhaps there's loose data in my current DB? How can I find it and delete it?

Ram Rachum
  • 84,019
  • 84
  • 236
  • 374

6 Answers6

86

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

Ram Rachum
  • 84,019
  • 84
  • 236
  • 374
  • 3
    Argh!!! One hour of my life wasted because of this error... and because of not finding this answer soon enough. Thank you! – webjunkie Jun 25 '12 at 15:55
  • This happened to me too! However---I'm still at a loss as to why some were created with MyISAM and other InnoDB. Do you know how this can happen? – jkeesh Mar 11 '13 at 21:37
  • 5
    Use `SHOW CREATE TABLE mytable;` for checking database engine. – Seppo Erviälä Mar 26 '13 at 16:08
  • I have the same error and follow your answer I check my DB all my tables are in innoDB but I still got this error. Does any one here have the same problem like I do? – gacon Oct 30 '13 at 09:33
  • 1
    `ALTER TABLE t1 ENGINE = InnoDB;` to change the engine – Pierre de LESPINAY Jan 02 '14 at 11:50
85
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.

mmrs151
  • 3,924
  • 2
  • 34
  • 38
18

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

ApPeL
  • 4,801
  • 9
  • 47
  • 84
8

Sometime the reason for this error is trying to save child table first and then save parent.
The solution for this is using.

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

2). Check your database operation flow and make it parent ---> child

GrvTyagi
  • 4,231
  • 1
  • 33
  • 40
6

I ran into this same issue: mmrs151's solution works, but NB that, for Django <= 1.2 (i.e. before multiple database support), the setting looks like this:

DATABASE_OPTIONS = {"init_command": "SET foreign_key_checks = 0;"}

Worth noting that Ram Rachum seems to have worked around rather than solved the problem: MyISAM doesn't support transactions at all...

supervacuo
  • 9,072
  • 2
  • 44
  • 61
-4

Another option is to drop the contraint in your MySQL table:

alter table <TABLE_NAME> drop foreign key <CONTRAINT_NAME>;
Nikolay Georgiev
  • 1,047
  • 1
  • 12
  • 22