I have a MySQL table and I want to add a foreign key constraint.
In this case it's a self-referencing key...
CREATE TABLE `contracts` (
  `id` int(11) NOT NULL auto_increment,
  `contract_id_amend_source` int(11) default NULL,
  # ...
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14834 DEFAULT CHARSET=latin1 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC;
(I'm working with a legacy DB here -- I didn't come up with the column names.)
I try to apply the key like this...
ALTER TABLE contracts
ADD CONSTRAINT fk_parent_id
FOREIGN KEY ( contract_id_amend_source )
REFERENCES contracts( id )
ON DELETE CASCADE;
But I get an error...
Cannot add or update a child row: a foreign key constraint fails (
contract_tracker/#sql-e18_e9d0a, CONSTRAINTfk_parent_idFOREIGN KEY (contract_id_amend_source) REFERENCEScontracts(id) ON DELETE CASCADE)
Ok, there are some invalid parent_ids.
I try to find them like this...
SELECT id, contract_id_amend_source
FROM contracts
WHERE contract_id_amend_source
NOT IN (
  SELECT id
  FROM contracts
);
That returns about 20 records. I fix the keys manually. Then run the above query again. Now it returns no records.
I try to ADD the FK again, but I still get the "Cannot add or update..." error.
I guess my query to find the bad keys must be wrong? How can I fix it?
 
     
    