MySQL Forums
Forum List  »  Newbie

Re: foreign key constraint fails
Posted by: Jon Erik Ween
Date: March 30, 2012 02:33PM

OK, is that what you need?

This works:

INSERT INTO `RHSSP`.`CmeList` (
`CeID` ,
`CeDateTime` ,
`CeEvCat` ,
`CeHours` ,
`CePresenter` ,
`CeDescript` ,
`CeLoc` ,
`CeDocName` ,
`CeDocSize` ,
`CeDocType`
)
VALUES (
NULL , '2012-03-30', '114', '2', 'jack', 'talk', 'Auditorium', NULL , NULL , NULL
);


This doesn't:

INSERT INTO `RHSSP`.`CmeList` (

`CeID` ,
`CeDateTime` ,
`CeEvCat` ,
`CeHours` ,
`CePresenter` ,
`CeDescript` ,
`CeLoc` ,
`CeDocName` ,
`CeDocSize` ,
`CeDocType`
)
VALUES (
NULL , '2012-03-30', '115', '2', 'jack', 'talk', 'Auditorium', NULL , NULL , NULL
)
MySQL said:

#1452 - Cannot add or update a child row: a foreign key constraint fails (`rhssp`.`cmelist`, CONSTRAINT `cmelist_ibfk_1` FOREIGN KEY (`CeEvCat`) REFERENCES `lookup` (`LuID`))


As long as CeEvCat (constrained to Lookup.LuID) is <115 we get the first behavior. Anything above 115 we fail. Here are the table structures:

CREATE TABLE Lookup (
LuID INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
LuCat VARCHAR(50),
LuVal VARCHAR(50),
INDEX (LuID)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE CmeList (
CeID INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
CeDateTime DATETIME NOT NULL,
CeEvCat INT UNSIGNED NOT NULL,
CeHours DECIMAL(3,2) UNSIGNED NOT NULL,
CePresenter TEXT NOT NULL,
CeDescription TEXT NOT NULL,
CeLoc TEXT NOT NULL,
CeDoc BLOB,
INDEX (CeEvCat, CeID),
FOREIGN KEY (CeEvCat) REFERENCES Lookup(LuID)
) ENGINE=InnoDB;

Thanks for helping!

Jon

Options: ReplyQuote


Subject
Written By
Posted
Re: foreign key constraint fails
March 30, 2012 02:33PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.