2
在發佈之前,我已經完成了一些關於此問題的搜索。 這裏是問題:即使存在引用的MySQL外鍵錯誤
mysql> insert into Buyer values (5594, CURDATE(), 490, 4830);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`oppminer`.`buyer`, CONSTRAINT `FK3D9119337C53076` FOREIGN KEY (`office_id`) REFERENCES `Office` (`id`))
我證實,所引用的行存在:
mysql> select * from Office where id = 490;
+-----+-----------+-----------+-------------+-------------------+
| id | name | agency_id | location_id | primaryContact_id |
+-----+-----------+-----------+-------------+-------------------+
| 490 | Top Level | 88 | 7363 | 451 |
+-----+-----------+-----------+-------------+-------------------+
1 row in set (0.00 sec)
下面是表描述:
| Buyer | CREATE TABLE `Buyer` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`created` datetime DEFAULT NULL,
`office_id` bigint(20) DEFAULT NULL,
`primaryContact_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK3D9119337C53076` (`office_id`),
KEY `FK3D911938ADC7080` (`primaryContact_id`),
CONSTRAINT `FK3D911938ADC7080` FOREIGN KEY (`primaryContact_id`) REFERENCES `Contact` (`id`),
CONSTRAINT `FK3D9119337C53076` FOREIGN KEY (`office_id`) REFERENCES `Office` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5595 DEFAULT CHARSET=latin1 |
+-------
| Office | CREATE TABLE `Office` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`agency_id` bigint(20) DEFAULT NULL,
`location_id` int(11) DEFAULT NULL,
`primaryContact_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FK8C9C2ADC3FBD3FD6` (`agency_id`),
KEY `FK8C9C2ADCFF56D2BF` (`location_id`),
KEY `FK8C9C2ADC8ADC7080` (`primaryContact_id`),
CONSTRAINT `FK8C9C2ADC8ADC7080` FOREIGN KEY (`primaryContact_id`) REFERENCES `Contact` (`id`),
CONSTRAINT `FK8C9C2ADC3FBD3FD6` FOREIGN KEY (`agency_id`) REFERENCES `Agency` (`id`),
CONSTRAINT `FK8C9C2ADCFF56D2BF` FOREIGN KEY (`location_id`) REFERENCES `Location` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=491 DEFAULT CHARSET=latin1 |
+--------
我還調查表中的索引:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
110503 12:32:12 Transaction:
TRANSACTION 17A89, ACTIVE 0 sec, OS thread id 4493770752 inserting
mysql tables in use 1, locked 1
1 lock struct(s), heap size 376, 0 row lock(s), undo log entries 1
MySQL thread id 17, query id 31610 localhost root update
insert into Buyer values (5594, CURDATE(), NULL, 4830)
Foreign key constraint fails for table `oppminer`.`buyer`:
CONSTRAINT `FK3D911938ADC7080` FOREIGN KEY (`primaryContact_id`) REFERENCES `Contact` (`id`)
Trying to add to index `FK3D911938ADC7080` tuple:
DATA TUPLE: 2 fields;
0: len 8; hex 80000000000012de; asc ;;
1: len 4; hex 800015da; asc ;;
But the parent table `oppminer`.`Contact`
or its .ibd file does not currently exist!
我有兩臺機器,我恢復了相同的轉儲。在工作機器上,我可以成功地執行相同的命令,從而在有問題的機器上導致錯誤。 任何想法,堆棧溢出?
轉儲說你試圖在最後一個外鍵未能插入'NULL'。 – 2011-05-04 04:16:09
我真的以爲我刪除了這篇文章,但對於任何可能遇到此問題的人來說,問題是MySQL Leopard 5.5.11 GA有一些主要問題。降級到5.5.8解決了這些問題。 – 2011-05-11 22:28:06