所以我在這裏遇到了一些問題,我似乎無法解決問題。我正在創建一個新表Assessment_Subsection_To_Group
,然後我需要添加一個列和外鍵給Assessment_Subsection
,這將創建它們之間的鏈接。努力添加這個mysql外鍵。
我到目前爲止的代碼如下所示。
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 ;;
在此先感謝。
欣賞解釋和隱喻;),我嘗試了你的建議,但它仍然給我錯誤。 – martynthewolf 2012-08-10 18:26:50
它只是我,還是你正在試圖添加到表中的字段,並且你試圖將它自己添加到它?請告訴我,這只是一個星期五的事情,我需要在週六早上有一個良好的睡眠? – 2012-08-10 18:48:41
Errm我不認爲我在做什麼。如果我試圖添加外鍵到表中填充0,它會導致問題嗎? – martynthewolf 2012-08-10 18:57:50