CodeIgniter Forums
Database Foreign key Problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Database Foreign key Problem (/showthread.php?tid=61034)



Database Foreign key Problem - El Forum - 09-02-2014

[eluser]rainfall[/eluser]

This problem occurred when i trying to insert data in tbl_add_designation table


Code:
Error Number: 1452

Cannot add or update a child row: a foreign key constraint fails (`db_erp_hr`.`tbl_add_designation`, CONSTRAINT `FK1_designation` FOREIGN KEY (`dept_id`) REFERENCES `tbl_add_department` (`dept_id`))

INSERT INTO `tbl_add_designation` (`desig_name`, `dept_name`) VALUES ('IT Officer', 'Admin ')

detp_id is the foreign key for department table .

How to solve it ??


Database Foreign key Problem - El Forum - 09-02-2014

[eluser]CroNiX[/eluser]
You'd be better off googling that standard mysql error and learn about it. Hard to answer exactly why it happens for you without knowing your db schema. But if I had to guess the dept_id does not exist in your department table, so you can't use it in another table that has the fk constraint.