i know im doing something stupid here but its been a few years since i worked with foreign keys, projects i was always on we did it with code not the db. here are my tables:
delimiter $$
CREATE TABLE `show_survey_name` (
`idSHOW_survey_name` int(11) NOT NULL auto_increment,
`name` varchar(255) NOT NULL COMMENT 'the name of the survey',
`start_date` date default NULL COMMENT 'the start date for the survey',
`end_date` date default NULL COMMENT 'end date for the survey',
`deleted` tinyint(1) NOT NULL default '0' COMMENT 'has the survey been deleted',
`survey_launch_location` varchar(255) default NULL COMMENT 'where in showpro does this survey pop up.',
`optional` tinyint(1) NOT NULL default '0',
PRIMARY KEY (`idSHOW_survey_name`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$
delimiter $$
CREATE TABLE `show_store_location` (
`store_id` int(11) NOT NULL auto_increment,
`clientID` int(11) default '0',
`store_code` varchar(4) default NULL,
`store_name` varchar(255) default NULL,
`store_link` varchar(255) default NULL,
`store_email` varchar(255) default NULL,
`store_phone` varchar(50) default NULL,
`store_fax` varchar(50) NOT NULL default '',
`store_contact` varchar(255) default NULL,
`region_code` varchar(5) default NULL,
`store_update_code` varchar(50) default NULL,
`Deleted` int(11) NOT NULL default '0',
`unbock_term_id` varchar(45) default NULL,
`north_dealerid` varchar(45) default NULL,
`north_clientid` varchar(45) default NULL,
`tide_dealerid` varchar(45) default NULL,
`tide_msgtype` varchar(45) default NULL,
`tide_dlrname` varchar(45) default NULL,
`smart_dlrname` varchar(45) default NULL,
PRIMARY KEY (`store_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$
delimiter $$
CREATE TABLE `show_survey_name_store` (
`idSHOW_survey_name_store` int(11) NOT NULL,
`SHOW_survey_name_idSHOW_survey_name` int(11) NOT NULL,
`SHOW_store_location_store_id` int(11) NOT NULL,
PRIMARY KEY (`idSHOW_survey_name_store`),
KEY `fk_SHOW_survey_name_store_SHOW_survey_name1` (`SHOW_survey_name_idSHOW_survey_name`),
KEY `fk_SHOW_survey_name_store_SHOW_store_location1` (`SHOW_store_location_store_id`),
CONSTRAINT `fk_SHOW_survey_name_store_SHOW_store_location1` FOREIGN KEY (`SHOW_store_location_store_id`) REFERENCES `show_store_location` (`store_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_SHOW_survey_name_store_SHOW_survey_name1` FOREIGN KEY (`SHOW_survey_name_idSHOW_survey_name`) REFERENCES `show_survey_name` (`idSHOW_survey_name`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1$$
when i try to do a simple insert into the linking table that links stores to surveys,
insert into SHOW_survey_name_store (SHOW_store_location_store_id, idSHOW_survey_name_store) values (3, 3)
i get
java.sql.BatchUpdateException: Cannot add or update a child row: a foreign key constraint fails (`showroom/show_survey_name_store`, CONSTRAINT `fk_SHOW_survey_name_store_SHOW_store_location1` FOREIGN KEY (`SHOW_store_location_store_id`) REFERENCES `show_store_location` (`store_id`) ON DEL)
i know im probably doing something simple wrong, and yes, record 3 exists in the store and the survey table. it should insert, i don’t see why its not.
thanks guys
Ok, first of all, I don’t know how you managed to create these tables because you have
show_store_locationset asMyISAMwhich doesn’t work with foreign keys. MySQL didn’t let me create it until I changed that toInnoDB. I also had to change the creation order of the tables. (I have edited your question to address both of these issues)Secondly, your actual problem is not with your foreign key, it’s with your horrible, horrible naming convention. I had to look at this for five minutes and sort through all the various verbal combinations of “show”, “store” and “id” to realize that you are just plain using the wrong column name in the insert (2nd one)
So, please, clean up your column and table names. Not only for yourself, but for the poor souls who have to look at this after you’ve moved on.
And lastly, I had to modify
show_survey_name_storelike so before the insert even workedYou may not want it to be auto-increment, and that’s fine, but then you’ll have to define a default value or remove the
NOT NULL.