I'm trying to add a foreign key in MySQL using MySQL Workbench. The code it is having me use is this:
ALTER TABLE `movies`.`camera`
ADD INDEX `fk_camera_movie_idx` (`movie_id` ASC);
ALTER TABLE `movies`.`camera`
ADD CONSTRAINT `fk_camera_movie`
FOREIGN KEY (`movie_id`)
REFERENCES `movies`.`movie` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE;
When I run the code I get this error:
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1452: Cannot add or update a child row: a foreign key constraint fails (movies
.#sql-e20_c5
, CONSTRAINT fk_camera_movie
FOREIGN KEY (movie_id
) REFERENCES movie
(id
) ON DELETE CASCADE ON UPDATE CASCADE)
I'm confused about the part that references movies
.#sql-e20_c5
. This is not the name of any of my tables, and as you can see the original code makes no reference to this name.
Can anyone provide any insight? Thank you.