所以我在这里遇到了一些问题,我似乎无法解决这个问题。我正在创建一个新表Assessment_Subsection_To_Group
,然后我需要添加一个列和外键,Assessment_Subsection
这将在它们之间创建一个链接。
我到目前为止的代码如下所示。
CREATE TABLE IF NOT EXISTS `Assessment_Subsection_To_Group` (
`assessment_subsection_to_group_id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
`title` VARCHAR(255) NOT NULL,
`time_created` DATETIME NOT NULL,
`time_modified` TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
`person_id_created` INT(11) UNSIGNED NOT NULL,
`person_id_modified` INT(11) UNSIGNED NOT NULL,
PRIMARY KEY (`assessment_subsection_to_group_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
ALTER TABLE `Assessment_Subsection`
ADD COLUMN `assessment_subsection_to_group_id` INT(11) UNSIGNED NOT NULL FIRST,
ADD FOREIGN KEY `assessment_subsection_to_group_id_ibfk` (`assessment_subsection_to_group_id`) REFERENCES `Assessment_Subsection_To_Group` (`assessment_subsection_to_group_id`);
当我尝试更改Assessment_Subsection
以尝试添加约束时,该表按预期创建问题。我收到此错误:
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`at`.<result 2 when explaining filename '#sql-47f_48'>, CONSTRAINT `assessment_subsection_to_group_id_ibfk` FOREIGN KEY (`assessment_subsection_to_group_id`) REFERENCES `Assessment_Subsection_)
然后我检查了show engine innodb status;
哪个给了我这个消息。我无法想象它的故事。sql 对我来说看起来不错,但我显然遗漏了一些东西。
ALTER TABLE `Assessment_Subsection` ADD COLUMN `assessment_subsection_to_group_id` INT (11) UNSIGNED NOT NULL FIRST, ADD CONSTRAINT `assessment_subsec
Foreign key constraint fails for table `at`.<result 2 when explaining filename '#sql-47f_48'>:
,CONSTRAINT `assessment_subsection_to_group_id_ibfk` FOREIGN KEY (`assessment_subsection_to_group_id`) REFERENCES `Assessment_Subsection_To_Group` (`
Trying to add in child table, in index `assessment_subsection_to_group_id_ibfk` tuple:
DATA TUPLE: 2 fields;
0: len 4; hex 00000000; asc ;;
1: len 4; hex 8000000b; asc ;;
But in parent table `at`.`Assessment_Subsection_To_Group`, in index `PRIMARY`,
the closest match we can find is record:
PHYSICAL RECORD: n_fields 1; compact format; info bits 0
0: len 8; hex 696e66696d756d00; asc infimum ;;
提前致谢。