Creating a foreign key in MySQL results in an error:

I am trying to create a foreign key in a table in MySQL, and I am getting a weird error that there seems to be little information about in any of my searches.

I create a key with this (emitted from mysql workbench 5.2):

ALTER TABLE `db`.`appointment` 
  ADD CONSTRAINT `FK_appointment_CancellationID`
  FOREIGN KEY (`CancellationID` ) REFERENCES `db`.`appointment_cancellation` (`ID` )
  ON DELETE NO ACTION
  ON UPDATE NO ACTION
, ADD INDEX `FK_appointment_CancellationID` (`CancellationID` ASC) ;

at what point do I get the error:

ERROR 1452: Unable to add or update child row: foreign key constraint fails with error ( alarmtekcore., CONSTRAINT FK_lead_appointment_CancellationIDFOREIGN KEY ( CancellationID) LINKS lead_appointment_cancellation(`)

I checked here

but there is no data in the table.

+3
source share
2 answers

, .

_, :

INSERT INTO appointment_cancellation
SELECT DISTINCT a.CancellationID
  FROM appointment
+4

- appointment. CancellationID appointment_cancellation. ID - . INT, - INT UNSIGNED, .

0

Source: https://habr.com/ru/post/1749108/


All Articles