MySQL约束在插入时失败,但在另一台服务器上的相同数据库中工作

时间:2013-02-01 22:58:42

标签: mysql

非常感谢任何人看看!

我有dev,stage和prod服务器。

我在dev上创建了我的数据库,然后做了一个mysqldump并在舞台和prod上安装了相同的数据库。

stage和prod是相同的硬件,os是最新的LAMP堆栈。

当我在prod上插入子行时,我收到错误,但它可以在我的dev和stage机器上运行。这很奇怪。 所以忽略它,这是我的简单数据(2个表)和我的错误:

   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 |

以下是适用的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 | heywood@jablome.com | $jxAB3sfe9CEixrNL3vKH6Jr7z. | Other |      1 | 2013-01-12 19:23:27 |
+-------------+----------------+--------+-----------+----------+----------------+---------------------+------------------------------------+----------------+--------+---------------------+
1 row in set (0.00 sec)

这是约束错误:有人比我更聪明,开导我吗?我真的很想知道是什么造成了这个!!

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)

以下是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!

1 个答案:

答案 0 :(得分:1)

好吧,其他人遇到了这个相同的问题,直到现在我才在这里找到它:MySQL Foreign Key Error Even With Existent References这导致我怀疑我用XAMPP打包的Mac版MySQL。这是5.1.44。有一个错误导致转储文件创建错误,并导致此错误。

切换到更高版本的mySQL解决了这个问题。