0

I went through a several QA's on stackoverflow in order to solve this for me but could not find. I did try all the answers from this question but it does not seem to help.

Whenever i try to update a record in table with a foreign key, I get the following error.

Mysql error 1452 - Cannot add or update a child row

what other factors might be causing this ? I'm using django 1.4, if that matters.

PS:

I tried the answers in the questions i mentioned and i always get an empty result as follows:

MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0004 sec )
Community
  • 1
  • 1
Amyth
  • 32,527
  • 26
  • 93
  • 135
  • 1
    It's because `FOREIGN KEY ('address_id')` is expected to have a corresponding value that you have inserted to `('id')`. `FOREIGN KEY ('address_id') REFERENCES 'marketing_prospectivecandidateaddress' ('id')` – catherine Mar 20 '13 at 07:52
  • it value does exist in `marketing_prospectivecandidateaddress` table – Amyth Mar 20 '13 at 08:04
  • Yeah, but you missed to put value for the reference table. Post the command codes that you use for updating the table – catherine Mar 20 '13 at 08:06
  • i simply use django models save method, you can find the code to the save method here : https://github.com/django/django/blob/master/django/db/models/base.py#L489 – Amyth Mar 20 '13 at 08:15
  • well actually you are correct while updating the record, for some reason the value of address is not being passed in the request. – Amyth Mar 20 '13 at 08:21
  • put your codes for update and we will try to find a solutionn – catherine Mar 20 '13 at 08:28

1 Answers1

1

If you insert or update a row on the first table the referenced address ID must exists on the second Table.
You can also add the following code to the foreign key constraint:

ON UPDATE CASCADE ON DELETE CASCADE


that means all modifications will be cascade to the child table

Zelldon
  • 5,396
  • 3
  • 34
  • 46