2014-01-22 173 views
0

錯誤:: 1452:Cannot add or update a child row: a foreign key constraint fails ( findmybuffet . det_res_item_selected , CONSTRAINT fk_det_res_item_selected_Buf_Off_Id1 FOREIGN KEY ( Buf_Off_Id ) REFERENCES mas_buf_off ( Buf_Off_Id ) ON DELETE CASCADE)錯誤::一個外鍵約束失敗


我有以下 ::

三個表

det_res_item_selecte d當我試圖創建表格時出現錯誤

我該如何解決這個問題?


CREATE TABLE `det_res_item_selected` (
    `Line_Selected_Item_Id` int(11) unsigned NOT NULL AUTO_INCREMENT, 
    `Item_Id` int(11) unsigned NOT NULL, 
    `Item_Selected` tinyint(4) DEFAULT '0', 
    `Buf_Off_Id` int(11) unsigned DEFAULT NULL, 
    `Item_Image` varchar(50) NOT NULL, 
    PRIMARY KEY (`Line_Selected_Item_Id`), 
    KEY `fk_det_res_item_selected_Item1_Idx` (`Item_Id`), 
    KEY `fk_det_res_item_selected_Buf_Off_Id1_Idx` (`Buf_Off_Id`), 
    CONSTRAINT `fk_det_res_item_selected_Buf_Off_Id1` FOREIGN KEY (`Buf_Off_Id`) REFERENCES `mas_buf_off` (`Buf_Off_Id`) ON DELETE CASCADE, 
    CONSTRAINT `fk_det_res_item_selected_Item_Id1` FOREIGN KEY (`Item_Id`) REFERENCES `mas_item` (`Item_Id`) ON DELETE CASCADE 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

CREATE TABLE `mas_buf_off` (
    `Buf_Off_Id` int(11) unsigned NOT NULL AUTO_INCREMENT, 
    `Buf_Type_Id` int(11) unsigned NOT NULL, 
    `From_Time` time NOT NULL, 
    `To_Time` time NOT NULL, 
    `Res_Id` int(11) unsigned NOT NULL, 
    PRIMARY KEY (`Buf_Off_Id`), 
    KEY `fk_mas_buf_off_Buf_Type_Id1_Idx` (`Buf_Type_Id`), 
    KEY `fk_mas_buf_off_Res_Id1_Idx` (`Res_Id`), 
    CONSTRAINT `fk_mas_buf_off_Buf_Type_Id1` FOREIGN KEY (`Buf_Type_Id`) REFERENCES `mas_buf_type` (`Buf_Type_Id`) ON DELETE CASCADE, 
    CONSTRAINT `fk_mas_buf_off_Res_Id1_Id1` FOREIGN KEY (`Res_Id`) REFERENCES `mas_restaurant` (`Res_Id`) ON DELETE CASCADE 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 

CREATE TABLE `mas_restaurant` (
    `Res_Id` int(11) unsigned NOT NULL AUTO_INCREMENT, 
    `Res_Name` varchar(100) NOT NULL, 
    `Res_Featured` tinyint(4) NOT NULL DEFAULT '0', 
    PRIMARY KEY (`Res_Id`) 
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; 
+0

這3個查詢中的哪一個導致錯誤?或者它是一些其他的查詢? –

+0

det_res_item_selected當我試圖創建表時拋出錯誤 – Devrath

回答

0

我只是跑下面的腳本,它的工作我的系統上。

set foreign_key_checks=0; 

CREATE TABLE `det_res_item_selected` (

    `Line_Selected_Item_Id` int(11) unsigned NOT NULL AUTO_INCREMENT, 

    `Item_Id` int(11) unsigned NOT NULL, 

    `Item_Selected` tinyint(4) DEFAULT '0', 

    `Buf_Off_Id` int(11) unsigned DEFAULT NULL, 

    `Item_Image` varchar(50) NOT NULL, 

    PRIMARY KEY (`Line_Selected_Item_Id`), 

    KEY `fk_det_res_item_selected_Item1_Idx` (`Item_Id`), 

    KEY `fk_det_res_item_selected_Buf_Off_Id1_Idx` (`Buf_Off_Id`), 

    CONSTRAINT `fk_det_res_item_selected_Buf_Off_Id1` FOREIGN KEY (`Buf_Off_Id`) REFERENCES `mas_buf_off` (`Buf_Off_Id`) ON DELETE CASCADE, 

    CONSTRAINT `fk_det_res_item_selected_Item_Id1` FOREIGN KEY (`Item_Id`) REFERENCES `mas_item` (`Item_Id`) ON DELETE CASCADE 

) ENGINE=InnoDB DEFAULT CHARSET=utf8; 



CREATE TABLE `mas_buf_off` (

    `Buf_Off_Id` int(11) unsigned NOT NULL AUTO_INCREMENT, 

    `Buf_Type_Id` int(11) unsigned NOT NULL, 

    `From_Time` time NOT NULL, 

    `To_Time` time NOT NULL, 

    `Res_Id` int(11) unsigned NOT NULL, 

    PRIMARY KEY (`Buf_Off_Id`), 

    KEY `fk_mas_buf_off_Buf_Type_Id1_Idx` (`Buf_Type_Id`), 

    KEY `fk_mas_buf_off_Res_Id1_Idx` (`Res_Id`), 

    CONSTRAINT `fk_mas_buf_off_Buf_Type_Id1` FOREIGN KEY (`Buf_Type_Id`) REFERENCES `mas_buf_type` (`Buf_Type_Id`) ON DELETE CASCADE, 

    CONSTRAINT `fk_mas_buf_off_Res_Id1_Id1` FOREIGN KEY (`Res_Id`) REFERENCES `mas_restaurant` (`Res_Id`) ON DELETE CASCADE 

) ENGINE=InnoDB DEFAULT CHARSET=utf8; 



CREATE TABLE `mas_restaurant` (

    `Res_Id` int(11) unsigned NOT NULL AUTO_INCREMENT, 

    `Res_Name` varchar(100) NOT NULL, 

    `Res_Featured` tinyint(4) NOT NULL DEFAULT '0', 

    PRIMARY KEY (`Res_Id`) 

) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; 

set foreign_key_checks=1;
+0

我解決了它.....我在父表中沒有數據,所以它拋出錯誤 – Devrath