MySQL INSERT fails with Composite Key of weak entitiy: "Duplicate entry 'x-y' for key 'PRIMARY'"

883 Views Asked by At

I'm having an issue INSERTING with the composite keys in my MySQL table DESP_Features_Weak.

I already took a look at MySQL composite unique on FK's, but my problem is a little bit different.

My ERD

INSERT INTO DESP_Features_Weak (details, Features_f_id, fk_desp_id, fk_dfws_id) VALUES (NULL, 2, 1, 5);

yields,

Error Code: 1062. Duplicate entry '1-2' for key 'PRIMARY'

Oddly however searching for a record with a 1,2 key (or even a 2,1 key) doesn't return any records, so there isn't a duplicate entry.

I read over in the MySQL documentation and a few people seem to have dropped their indexes to fixed their problem; I did the same and it doesn't seem to have changed anything, although I believe there is still an index named PRIMARY which I have been unable to drop.

This is just a personal database I've thrown together in MySQL Workbench, it's not in production or anything.

A dump of the related table looks like this:

CREATE TABLE `DESP_Features_Weak` (
  `DESP_Features_Weakcol` varchar(45) DEFAULT NULL,
  `Details` mediumtext,
  `fk_desp_id` int(11) NOT NULL,
  `Features_f_id` int(11) NOT NULL,
  `CostType_ct_id` int(11) NOT NULL,
  `cost_amount` decimal(18,2) DEFAULT NULL,
  `askedAboutFeatureFees` tinyint(1) DEFAULT NULL,
  `fk_dfws_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`fk_desp_id`,`Features_f_id`),
  KEY `fk_DESP_Features_Weak_Features1` (`Features_f_id`),
  KEY `fk_DESP_Features_Weak_DESP_Feature_Weak_State1` (`fk_dfws_id`),
  CONSTRAINT `fk_DESP_Features_Weak_DigitalEditionsSolutionProvider1` FOREIGN KEY (`fk_desp_id`) REFERENCES `DigitalEditionsSolutionProvider` (`desp_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_DESP_Features_Weak_DESP_Feature_Weak_State1` FOREIGN KEY (`fk_dfws_id`) REFERENCES `DESP_Feature_Weak_State` (`dfws_id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT `fk_DESP_Features_Weak_Features1` FOREIGN KEY (`Features_f_id`) REFERENCES `Features` (`f_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/
0

There are 0 best solutions below