即使存在引用也存在MySQL外键错误

时间:2011-05-03 19:52:18

标签: mysql foreign-keys

在发布此处之前,我已经对此问题进行了一些搜索。 这是问题所在:

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!

我有两台机器可以将同一个转储恢复到。在工作机器上,我可以成功执行导致有问题的机器出错的相同命令。 任何想法,堆栈溢出?

1 个答案:

答案 0 :(得分:1)

最近发布的(5月31日)MySQL 5.5.13也已修复 MySQL 5.5 foreign key constraint fails when foreign key exists