I have the following tables:
CREATE TABLE IF NOT EXISTS `Person_Categories` (
`PrsCatID` int(11) NOT NULL auto_increment,
`PrsCategory` varchar(45) NOT NULL,
PRIMARY KEY (`PrsCatID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=10 ;
CREATE TABLE IF NOT EXISTS `Persons` (
`PersonID` int(11) NOT NULL auto_increment,
`FirstName` varchar(45) NOT NULL,
`LastName` varchar(45) NOT NULL,
`OrderName` varchar(45) default NULL,
`Email` varchar(45) NOT NULL,
`Telephone` varchar(20) default NULL,
`Mobile` varchar(20) default NULL,
`StreetAddress` varchar(45) NOT NULL,
`City` varchar(45) NOT NULL,
`RegionID` int(2) NOT NULL,
`PostCode` varchar(10) NOT NULL,
`CountryID` int(11) NOT NULL,
`TitleID` int(11) NOT NULL,
`CIC_MailingList` tinyint(1) NOT NULL,
`FoundationMember` tinyint(1) NOT NULL,
`PersonCmts` mediumtext,
PRIMARY KEY (`PersonID`),
KEY `TitleID` (`TitleID`),
KEY `RegionID` (`RegionID`),
KEY `CountryID` (`CountryID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
CREATE TABLE IF NOT EXISTS `Persons_PersonCategories` (
`PersonID` int(11) NOT NULL,
`PrsCatID` int(11) NOT NULL,
PRIMARY KEY (`PersonID`,`PrsCatID`),
KEY `PrsCatID` (`PrsCatID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Constraints for the tables
--
ALTER TABLE `Persons`
ADD CONSTRAINT `Persons_ibfk_12` FOREIGN KEY (`TitleID`) REFERENCES `Job_Titles` (`TitleID`),
ADD CONSTRAINT `Persons_ibfk_14` FOREIGN KEY (`CountryID`) REFERENCES `Countries` (`CountryID`),
ADD CONSTRAINT `Persons_ibfk_15` FOREIGN KEY (`RegionID`) REFERENCES `Regions` (`RegionID`);
ALTER TABLE `Persons_PersonCategories`
ADD CONSTRAINT `Persons_PersonCategories_ibfk_8` FOREIGN KEY (`PrsCatID`) REFERENCES `Person_Categories` (`PrsCatID`),
ADD CONSTRAINT `Persons_PersonCategories_ibfk_7` FOREIGN KEY (`PersonID`) REFERENCES `Persons` (`PersonID`);
Persons_PersonCateogires is a linking table for a n:m relationship. When I try to insert PersonID and PrsCatID into the Persons_PersonCategories via my php application I get the following error:
An error occurs during insert:
Cannot add or update a child row: a foreign key constraint fails (
ubarry09_andrew/Persons_PersonCategories
, CONSTRAINTPersons_PersonCategories_ibfk_7
FOREIGN KEY (PersonID
) REFERENCESPersons
(PersonID
))
Here is the insert statement:
INSERT INTO Persons_PersonCategories
VALUES (PersonID, PrsCatID)
Persons and Persons_Categories tables are populated with data.
Many thanks, zan