views:

45

answers:

1

I am getting the error:

Cannot add or update a child row: a foreign key constraint fails (mydb/requests, CONSTRAINT requests_ibfk_5 FOREIGN KEY (fixture_id) REFERENCES fixtures (fix_id) ON UPDATE CASCADE ON DELETE CASCADE)

I have the following table structure:

CREATE TABLE IF NOT EXISTS `requests` (
  `request_id` int(11) unsigned NOT NULL auto_increment,
  `fixture_id` int(11) unsigned NOT NULL,
  `user_id` int(11) unsigned NOT NULL,
  `date_added` datetime NOT NULL,
  `date_modified` datetime default NULL,
  PRIMARY KEY  (`request_id`),
  UNIQUE KEY `fixture_id_2` (`fixture_id`,`user_id`),
  KEY `user_id` (`user_id`),
  KEY `date_added` (`date_added`),
  KEY `fixture_id` (`fixture_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=17 ;


CREATE TABLE IF NOT EXISTS `fixtures` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `fix_id` int(11) unsigned NOT NULL default '0',
  `fixture_date` date default NULL,
  `kickoff` time default NULL,
  `venue` varchar(35) default NULL,
  `home_score` tinyint(4) default NULL,
  `away_score` tinyint(4) default NULL,
  `date_added` datetime default NULL,
  `date_modified` datetime default NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `fix_id` (`fix_id`),
  KEY `fixture_date` (`fixture_date`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=383 ;


ALTER TABLE `requests`
  ADD CONSTRAINT `requests_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `users` (`user_id`) ON DELETE CASCADE,
  ADD CONSTRAINT `requests_ibfk_5` FOREIGN KEY (`fixture_id`) REFERENCES `fixtures` (`fix_id`) ON DELETE CASCADE ON UPDATE CASCADE;

If I update a record on the fix_id field the parent table (fixtures), that has a shared id (fixture_id) in the child table (requests) I get the above error.

I cannot see why this integrity constraint is failing. Both tables already have the correct data it should cascade through?

Any help greatly appreciated.

A: 

This was all my own error. I had two foreign constraints on the same field in reality. I just needed to take one off.

Stephen Maher