一尘不染

存在外键时,MySQL 5.5外键约束失败

mysql

刚在Mac OS X 10.6上安装了MySQL
5.5,并且在许多表上都有一个奇怪的问题。下面是一个例子。插入行失败,但不应插入外键约束。它引用的外键确实存在。有任何想法吗?

mysql> show create table Language;
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Language | CREATE TABLE `Language` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `Code` varchar(2) NOT NULL,
  `Name` varchar(63) CHARACTER SET utf8 DEFAULT NULL,
  `Variant` varchar(63) CHARACTER SET utf8 DEFAULT NULL,
  `Country_Id` int(11) DEFAULT NULL,
  PRIMARY KEY (`Id`),
  UNIQUE KEY `Code` (`Code`,`Country_Id`,`Variant`),
  KEY `FKA3ACF7789C1796EB` (`Country_Id`),
  CONSTRAINT `FKA3ACF7789C1796EB` FOREIGN KEY (`Country_Id`) REFERENCES `Country` (`Id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1 |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> show create table Language_Phrases;
+------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table            | Create Table                                                                                                                                                                                                                                                                                                                                                    |
+------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Language_Phrases | CREATE TABLE `Language_Phrases` (
  `Language_Id` int(11) NOT NULL,
  `Phrase` varchar(255) DEFAULT NULL,
  `Label` varchar(255) NOT NULL,
  PRIMARY KEY (`Language_Id`,`Label`),
  KEY `FK8B4876F3AEC1DBE9` (`Language_Id`),
  CONSTRAINT `FK8B4876F3AEC1DBE9` FOREIGN KEY (`Language_Id`) REFERENCES `Language` (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

mysql> select * from Language;
+----+------+----------+---------+------------+
| Id | Code | Name     | Variant | Country_Id |
+----+------+----------+---------+------------+
|  1 | en   | English  |         |        235 |
|  2 | ro   | Romanian |         |        181 |
+----+------+----------+---------+------------+
2 rows in set (0.00 sec)

mysql> select * from Language_Phrases;
Empty set (0.00 sec)

mysql> INSERT INTO Language_Phrases (Language_Id, Label, Phrase) VALUES (1, 'exampleLabel', 'Some phrase');
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`dev`.`language_phrases`, CONSTRAINT `FK8B4876F3AEC1DBE9` FOREIGN KEY (`Language_Id`) REFERENCES `Language` (`Id`))
mysql>

更新 :多次删除并重新创建数据库show engine innodb status后,我在上述插入失败后进行了操作,并得到了令人惊讶的结果。找不到父语言表!这似乎很奇怪…有什么想法吗?

------------------------
LATEST FOREIGN KEY ERROR
------------------------
110406  9:55:49 Transaction:
TRANSACTION CA3B, ACTIVE 0 sec, OS thread id 4494462976 inserting
mysql tables in use 1, locked 1
1 lock struct(s), heap size 376, 0 row lock(s)
MySQL thread id 25, query id 50720 localhost root update
INSERT INTO Language_Phrases (Language_Id, Label, Phrase) VALUES (1, 'exampleLabel', 'Some phrase')
Foreign key constraint fails for table `dev`.`language_phrases`:
,
  CONSTRAINT `FK8B4876F3AEC1DBE9` FOREIGN KEY (`Language_Id`) REFERENCES `Language` (`Id`)
Trying to add to index `PRIMARY` tuple:
DATA TUPLE: 5 fields;
 0: len 4; hex 80000001; asc     ;;
 1: len 17; hex 747970654d69736d617463682e79656172; asc exampleLabel;;
 2: len 6; hex 00000000ca3b; asc      ;;;
 3: len 7; hex 00000000000000; asc        ;;
 4: len 21; hex 59656172206d7573742062652061206e756d626572; asc Some phrase;;

But the parent table `dev`.`Language`
or its .ibd file does not currently exist!

更新2 :事实证明,这只是MySQL中的一个 巨大 错误。显然,最新版本的MySQL无法在mac os X
10.6下完全运行(也许也可以是早期版本?)。降级至5.5.8似乎可行。非常令人惊讶。


阅读 271

收藏
2020-05-17

共1个答案

一尘不染

这似乎是自Mac OS X上的MySQL
5.5.9起引入的错误:http
://bugs.mysql.com/bug.php?id=60309

它在5.5.13(5月31日发布)中被标记为已修复,并在发行说明中提到:http
:
//dev.mysql.com/doc/refman/5.5/en/news-5-5-13.html

另外,在错误报告中列出了一种解决方法,我已在5.5.10上对此进行了验证,并复制如下:

[3 Mar 20:29] Harald Neiss

我还收到了一个新的MBP并重新安装了MySQL(mysql-5.5.10-osx10.6-x86_64)。最后我
遇到了与上述相同的问题。所以这是查询结果以及我
确实解决了。

mysql>显示类似“ lower%”的变量;
+ ------------------------ + ------- +
| 变量名| 价值|
+ ------------------------ + ------- +
| lower_case_file_system | 开|
| lower_case_table_names | 2 |
+ ------------------------ + ------- +
设置2行(0.00秒)

删除数据库,创建文件/etc/my.cnf,其内容如下:

[mysqld]
lower_case_table_names = 1

重新启动MySQL守护程序并重复查询:

mysql>显示类似“ lower%”的变量;
+ ------------------------ + ------- +
| 变量名| 价值|
+ ------------------------ + ------- +
| lower_case_file_system | 开|
| lower_case_table_names | 1 |
+ ------------------------ + ------- +
设置2行(0.00秒)

我重新创建了表,一切正常。
2020-05-17