wclark
wclark

Reputation: 436

MySQL Constraint Fails on insert, but works in identical database on another server

A quick thanks to anyone taking a look!

I have dev, stage and prod servers.

I created my database on dev, then did a mysqldump and installed the same database on stage and prod.

stage and prod are identical hardware, and os, which is latest LAMP stack.

When I insert a child row on prod, I get an error, yet it works on my dev and stage machines. That is weird. So ignoring that, here is my simple data (2 tables) and my error:

   CREATE TABLE `Customer` (
  `Customer_Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `AccessLevel_Id` int(11) unsigned NOT NULL,
  `UserId` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  `FirstName` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  `LastName` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
  `Phone` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
  `Email` varchar(100) COLLATE utf8_unicode_ci NOT NULL,
  `Passwd` tinytext COLLATE utf8_unicode_ci NOT NULL,
  `HearAbout` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `Active` tinyint(1) NOT NULL DEFAULT '1',
  `Created` datetime NOT NULL,
  PRIMARY KEY (`Customer_Id`),
  UNIQUE KEY `UserId` (`UserId`),
  KEY `idxEmail` (`Email`),
  KEY `R8` (`AccessLevel_Id`),
  CONSTRAINT `R8` FOREIGN KEY (`AccessLevel_Id`) REFERENCES `accesslevel` (`AccessLevel_Id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |

CREATE TABLE `Recipient` (
  `Recipient_Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `Customer_Id` int(11) unsigned NOT NULL,
  `FirstName` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `LastName` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `NickName` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `LastUsed` datetime DEFAULT NULL,
  `Active` tinyint(1) NOT NULL DEFAULT '1',
  `Created` datetime NOT NULL,
  PRIMARY KEY (`Recipient_Id`),
  KEY `R14` (`Customer_Id`),
  CONSTRAINT `R14` FOREIGN KEY (`Customer_Id`) REFERENCES `customer` (`Customer_Id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci |

Here is the applicable id.

mysql> select * from Customer where Customer_Id=1;
+-------------+----------------+--------+-----------+----------+----------------+---------------------+------------------------------------+----------------+--------+---------------------+
| Customer_Id | AccessLevel_Id | UserId | FirstName | LastName | Phone          | Email               | Passwd                             | HearAbout      | Active | Created             |
+-------------+----------------+--------+-----------+----------+----------------+---------------------+------------------------------------+----------------+--------+---------------------+
|           1 |              3 | userid | Heywood   | Jablome    | (313) 248-1234 | [email protected] | $jxAB3sfe9CEixrNL3vKH6Jr7z. | Other |      1 | 2013-01-12 19:23:27 |
+-------------+----------------+--------+-----------+----------+----------------+---------------------+------------------------------------+----------------+--------+---------------------+
1 row in set (0.00 sec)

Here is the constraint error: Can someone wiser than me, enlighten me? I'd really like to know what is causing this!!

mysql> INSERT INTO `Recipient` (`Customer_Id`, `NickName`, `FirstName`, `LastName`, `Created`) VALUES ('1', 'Moe', 'Moe', 'Howard', '2013-02-01 15:25:19');
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`db1`.`Recipient`, CONSTRAINT `R14` FOREIGN KEY (`Customer_Id`) REFERENCES `customer` (`Customer_Id`) ON DELETE CASCADE ON UPDATE CASCADE)

Here is the result of show innodb status \G

------------------------
LATEST FOREIGN KEY ERROR
------------------------
130201 16:56:25 Transaction:
TRANSACTION 0 3429, ACTIVE 0 sec, process no 8699, OS thread id 3245587822336 inserting, thread declared inside InnoDB 500
mysql tables in use 1, locked 1
1 lock struct(s), heap size 368, 0 row lock(s), undo log entries 1
MySQL thread id 309, query id 97882 localhost root update
INSERT INTO `Recipient` (`Customer_Id`, `NickName`, `FirstName`, `LastName`, `Created`) VALUES ('1', 'Moe', 'Moe', 'Howard', '2013-02-01 15:25:19')
Foreign key constraint fails for table `db1`.`Recipient`:
,
  CONSTRAINT `R14` FOREIGN KEY (`Customer_Id`) REFERENCES `customer` (`Customer_Id`) ON DELETE CASCADE ON UPDATE CASCADE
Trying to add to index `R14` tuple:
DATA TUPLE: 2 fields;
 0: len 4; hex 00000001; asc     ;; 1: len 4; hex 0000000a; asc     ;;

But the parent table `db1`.`customer`
or its .ibd file does not currently exist!

Upvotes: 0

Views: 170

Answers (1)

wclark
wclark

Reputation: 436

OK, someone else ran into this identical problem, and I didn't find it here until now: MySQL Foreign Key Error Even With Existent References and this led me to suspect the Mac version of MySQL I had packaged with XAMPP. It is 5.1.44. There is a bug that causes the dump file to be created incorrectly, and cause this error.

Switching to a later version of mySQL fixed the problem.

Upvotes: 1

Related Questions