2011-04-10 71 views
0

嘿傢伙,我得到這個錯誤。從myISAM轉換到innodb的Mysql錯誤

Error 1452 : Cannot add or update a child row: a foreign key constraint fails (`s2794971db/ProfileInterests`, CONSTRAINT `ProfileInterests_ibfk_2` FOREIGN KEY (`InterestID`) REFERENCES `Interests` (`ID`)) 

我把我的表從myISAM更改爲innodb ...發現我需要這樣刪除更容易。

我有問題,所以我刪除了我需要創建關係的表。

然後我又使它

我本來

create table if not exists Users (
    ID int not null auto_increment primary key, 
    FirstName varchar(40) not null, 
    LastName varchar(40) not null, 
    UserName varchar(40) not null, 
    UserEmail varchar(40) not null, 
    UserDOB timestamp not null, 
    UserJoin datetime not null 
); 

create table if not exists Interests(
    ID int not null auto_increment primary key, 
    Interests varchar(40) not null 
); 

create table if not exists ProfileInterests (
    userID int not null References Users(ID), 
    InterestID int not null References Interests(ID), 
    MiddleID int not null auto_increment primary key 
); 

但後來我刪除了最後一個表,並使其

create table if not exists ProfileInterests (
    userID int not null, 
    InterestID int not null, 
    MiddleID int not null auto_increment primary key 
); 

,然後我做了用戶ID和InterestID進入指數的和然後我添加了關係User-> ID爲userID和interest-> ID爲interestID

當我試圖通過php表單輸入數據時發生錯誤。

任何想法...真的需要一些幫助!

回答

1

很顯然,您正嘗試在表中沒有匹配記錄的ProfileInterests中插入一條記錄。查看確切的插入查詢,並檢查是否爲表中的每個字段提供了一個有效值,這是外鍵關係的一部分。