MySQL Forums
Forum List  »  InnoDB

Error 1452
Posted by: Ali Fatolahi
Date: August 14, 2008 07:27AM

Hi,

I'm using MySQL Query Browser and I have two tables:

Candidate and Poll

CREATE TABLE `elections`.`poll` (
`id` int(10) unsigned NOT NULL auto_increment,
`pollname` varchar(45) NOT NULL,
`election_fk` int(10) unsigned NOT NULL,
`polldate` datetime NOT NULL,
`pollplace` varchar(45) NOT NULL,
`pollstatus` varchar(45) NOT NULL,
PRIMARY KEY USING BTREE (`id`),
CONSTRAINT `election_fk` FOREIGN KEY (`id`) REFERENCES `election` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

CREATE TABLE `elections`.`candidate` (
`id` int(10) unsigned NOT NULL auto_increment,
`candidatename` varchar(45) NOT NULL,
`poll_fk` int(10) unsigned NOT NULL,
PRIMARY KEY USING BTREE (`id`,`poll_fk`),
CONSTRAINT `poll_fk` FOREIGN KEY (`id`) REFERENCES `poll` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

I have two rows in my Poll table one with ID, 1 and the other 2.

I tried the following:
insert into Candidate values(1, 'name', 1);
insert into Candidate values(2, 'name', 1);
insert into Candidate values(3, 'name', 2);

The first two succeeded but on the last one I recieve the following error:
Cannot add or update a child row: a foreign key constraint fails (`elections/candidate`, CONSTRAINT `poll_fk` FOREIGN KEY (`id`) REFERENCES `poll` (`id`))

The same thing happened when trying to insert rows in the Poll table, which has a foreign key to Elections table. After two successfull insertion it stopped working with the same error.

Is this a bug that I cannot add more than two rows? or is it because of something else?

Options: ReplyQuote


Subject
Views
Written By
Posted
Error 1452
8370
August 14, 2008 07:27AM
3632
December 29, 2008 12:10PM


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.