A Foreign Key Constraint Fails

Currently on page 120, but I cannot seem to run the query to manually assign the two users to Project 1. Everytime I run it I get an error that states:


#1452 - Cannot add or update a child row: a foreign key constraint fails (`trackstar_dev`.`tbl_project_user_assignment`, CONSTRAINT `FK_project_user` FOREIGN KEY (`project_id`) REFERENCES `tbl_project` (`id`) ON DELETE CASCADE)

I’m not entirely sure why this is failing as I set the foreign keys according to what the book said.




ALTER TABLE tbl_project_user_assignment

ADD CONSTRAINT FK_project_user FOREIGN KEY

(project_id)

REFERENCES tbl_project(id)

ON DELETE CASCADE

ON UPDATE RESTRICT

;


ALTER TABLE tbl_project_user_assignment

ADD CONSTRAINT FK_user_project FOREIGN KEY

(user_id)

REFERENCES tbl_user(id)

ON DELETE CASCADE

ON UPDATE RESTRICT

;

Any help in resolving this issue would be greatly appreciated.

I ended up figuring it out. It appears that I did not have any projects created, so the query was failing. Once I created a couple projects everything worked correctly.