我试图看看另一个人的问题,但他们似乎与我的不同。
我的问题是我一直收到ERROR 1452问题“ERROR 1452(23000):无法添加或更新子行:外键约束失败”即使我认为外键受到约束。
我的桌子是
CREATE TABLE `Users` (
`id` int(11) unsigned NOT NULL,
`username` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`joinTime` datetime DEFAULT NULL,
`isRemoved` tinyint(3) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `username` (`username`),
KEY `isRemoved` (`isRemoved`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE `RoleGroups` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`name` text NOT NULL,
`description` text NOT NULL,
`createdTime` datetime NOT NULL,
`lastUpdate` datetime DEFAULT NULL,
`isRemoved` tinyint(3) unsigned NOT NULL DEFAULT '0',
PRIMARY KEY (`id`),
KEY `isRemoved` (`isRemoved`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1
CREATE TABLE `RoleGroupsUsers` (
`groupId` int(11) unsigned NOT NULL,
`userId` int(11) unsigned NOT NULL,
`addedTime` datetime NOT NULL,
KEY `fkGroup_groupId` (`groupId`),
KEY `fkGroup_userId` (`userId`),
CONSTRAINT `fkGroup_groupId` FOREIGN KEY (`groupId`) REFERENCES `RoleGroups` (`id`),
CONSTRAINT `fkGroup_userId` FOREIGN KEY (`userId`) REFERENCES `Users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
我隐藏了Users表中的一些属性以简化问题。
我目前的数据是
mysql> SELECT id, username, joinTime FROM Users ORDER BY joinTime ASC;
+----------+-----------+---------------------+
| id | username | joinTime |
+----------+-----------+---------------------+
| 1 | admin | 2011-07-22 01:20:29 |
| 2 | johndoe | 2011-07-22 01:26:07 |
| 3 | janedoe | 2011-07-22 01:26:07 |
| 31201958 | yonas1 | 2011-07-22 01:32:38 |
| 49494052 | bvnarayan | 2011-07-22 11:51:05 |
+----------+-----------+---------------------+
5 rows in set (0.00 sec)
mysql> SELECT * FROM RoleGroups;
+----+---------------+---------------+---------------------+------------+-----------+
| id | name | description | createdTime | lastUpdate | isRemoved |
+----+---------------+---------------+---------------------+------------+-----------+
| 1 | member | Member | 2011-07-22 01:20:29 | NULL | 0 |
| 2 | administrator | Administrator | 2011-07-22 01:20:29 | NULL | 0 |
+----+---------------+---------------+---------------------+------------+-----------+
2 rows in set (0.00 sec)
mysql> SELECT * FROM RoleGroupsUsers;
+---------+----------+---------------------+
| groupId | userId | addedTime |
+---------+----------+---------------------+
| 2 | 1 | 2011-07-22 01:20:29 |
| 1 | 1 | 2011-07-22 01:20:29 |
| 1 | 2 | 2011-07-22 01:26:07 |
| 1 | 3 | 2011-07-22 01:26:07 |
| 1 | 31201958 | 2011-07-22 01:32:38 |
+---------+----------+---------------------+
5 rows in set (0.00 sec)
当我尝试
时弹出错误mysql> INSERT INTO RoleGroupsUsers (groupId, userId, addedTime) VALUES (1, 49494052, '2011-07-22 14:51:05');
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`roswell`.`rolegroupsusers`, CONSTRAINT `fkGroup_groupId` FOREIGN KEY (`groupId`) REFERENCES `RoleGroups` (`id`))
这真的很奇怪,因为在RoleGroups表中已经有一行ID为1.而更令人奇怪的是,这个错误在我的一个工作站中不断发生而另一个运行良好。我的开发工作站使用Windows 7和XAMPP服务器。我的其他工作站使用OS X和MAMP。
更新
几小时前它在第二个工作站工作但我不知道为什么它现在不起作用。 (可能是因为我在第二个工作站中做了一些我不知道的配置)
再次更新
MySQL版
#/Applications/MAMP/Library/bin/mysql --version
/Applications/MAMP/Library/bin/mysql Ver 14.14 Distrib 5.5.9, for osx10.4 (i386) using EditLine wrapper
答案 0 :(得分:4)
您需要在OSX计算机上升级MySQL。这是OSX上MySQL版本5.5.9到5.5.12中的一个错误。
请参阅:MySQL bug report,MySQL 5.5.13 patch notes(参考第2项)