我的模型上有这些表格:
DROP TABLE IF EXISTS `UNIVERSES`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `UNIVERSES` (
`country_code` varchar(2) COLLATE utf8_unicode_ci NOT NULL,
`code` varchar(2) COLLATE utf8_unicode_ci NOT NULL,
`description` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`image` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`order` tinyint(4) NOT NULL,
PRIMARY KEY (`country_code`,`code`),
KEY `identifier` (`code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
ALTER TABLE `UNIVERSES`
ADD CONSTRAINT `UNIVERSES_ibfk_1` FOREIGN KEY (`country_code`) REFERENCES `COUNTRY` (`id`) ON UPDATE CASCADE;
DROP TABLE IF EXISTS `RULES`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `RULES` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`from_element_code` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
`appliedto_element_code` varchar(45) COLLATE utf8_unicode_ci NOT NULL,
`type` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
ALTER TABLE `RULES`
ADD CONSTRAINT `RULES_ibfk_1` FOREIGN KEY (`from_element_code`) REFERENCES `ELEMENTS` (`code`) ON UPDATE CASCADE,
ADD CONSTRAINT `RULES_ibfk_2` FOREIGN KEY (`appliedto_element_code`) REFERENCES `ELEMENTS` (`code`) ON UPDATE CASCADE,
ADD CONSTRAINT `RULES_ibfk_3` FOREIGN KEY (`type`) REFERENCES `RULES_TYPE` (`id`) ON UPDATE CASCADE;
我需要添加一个带约束的字段。我试试:
--
-- Add new field to RULES table
--
ALTER TABLE `RULES` ADD `univers_code` VARCHAR(2) COLLATE utf8_unicode_ci NOT NULL AFTER `appliedto_element_code`;
ALTER TABLE `RULES` ADD `country_code` VARCHAR(2) COLLATE utf8_unicode_ci NOT NULL AFTER `appliedto_element_code`;
--
-- Create new index for new RULES field
--
ALTER TABLE `RULES`
ADD CONSTRAINT `RULES_ibfk_4` FOREIGN KEY (`country_code`, `univers_code`) REFERENCES `UNIVERSES` (`country_code`, `code`) ON UPDATE CASCADE;
并且创建了字段,但约束失败并出现以下错误:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`CFS_DEV`.<result 2 when explaining filename '#sql-aac_145'>, CONSTRAINT `RULES_ibfk_4` FOREIGN KEY (`country_code`, `univers_code`) REFERENCES `UNIVERSES` (`country_code`, `code`) ON UPDATE C)
我已经看过类型匹配,并且由于它是双主键,因此该对是唯一的,实际上它是引用表的主键。我不知道自己在逃学。
请帮助我们。
提前致谢。
编辑:mysql引擎输出
------------------------
LATEST FOREIGN KEY ERROR
------------------------
140826 18:55:17 Transaction:
TRANSACTION 25649B, ACTIVE 0 sec, process no 2732, OS thread id 2825907056 inserting
mysql tables in use 2, locked 2
5 lock struct(s), heap size 320, 2 row lock(s), undo log entries 1
MySQL thread id 349, query id 19960 192.168.230.1 mdecfs01 copy to tmp table
ALTER TABLE `RULES`
ADD CONSTRAINT `RULES_ibfk_4` FOREIGN KEY (`univers_code`) REFERENCES `UNIVERSES` (`code`) ON DELETE CASCADE ON UPDATE CASCADE
Foreign key constraint fails for table `CFS_DEV`.<result 2 when explaining filename '#sql-aac_15d'>:
,
CONSTRAINT `RULES_ibfk_4` FOREIGN KEY (`univers_code`) REFERENCES `UNIVERSES` (`code`) ON DELETE CASCADE ON UPDATE CASCADE
Trying to add in child table, in index `RULES_ibfk_4` tuple:
DATA TUPLE: 2 fields;
0: len 0; hex ; asc ;;
1: len 4; hex 80000023; asc #;;
But in parent table `CFS_DEV`.`UNIVERSES`, in index `identifier`,
the closest match we can find is record:
PHYSICAL RECORD: n_fields 2; compact format; info bits 0
0: len 1; hex 41; asc A;;
1: len 2; hex 6265; asc be;;