MySQL Forums
Forum List  »  Newbie

Getting a `duplicate key` exception while adding a FK to the child table
Posted by: Jasmine Chacko
Date: February 28, 2014 01:18PM

I have a parent table called 'member' and the PK is Unsigned, autogenerated number named as 'memberId'.
In my second table called 'fine', I need to add a field called 'memberId' which will be an FK to member table. (I kept 'memberId'in fine table as the same datatype and also unsigned.

But I get the following error when I try to add this FK in fine table.

Please see below:

ERROR 1022: Can't write; duplicate key in table '#sql-2260_12'

SQL Statement:

ALTER TABLE `library`.`fine`
ADD CONSTRAINT `memberId`
FOREIGN KEY (`memberId`)
REFERENCES `library`.`member` (`memberId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION

ERROR: Error when running failback script. Details follow.

ERROR 1050: Table 'fine' already exists

SQL Statement:

CREATE TABLE `fine` (
`fineId` int(11) unsigned NOT NULL,
`description` varchar(45) DEFAULT NULL,
`fineAmount` decimal(10,0) DEFAULT NULL,
`fineCollectedDate` date DEFAULT NULL,
`memberId` int(11) unsigned NOT NULL,
PRIMARY KEY (`fineId`),
UNIQUE KEY `fineId_UNIQUE` (`fineId`),
KEY `member_ID_idx` (`memberId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Options: ReplyQuote


Subject
Written By
Posted
Getting a `duplicate key` exception while adding a FK to the child table
February 28, 2014 01:18PM


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.