0

Error: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row:

Error: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`nclinicn`.`patients`, CONSTRAINT `fk_dept_id` FOREIGN KEY (`dept_id`) REFERENCES `departments` (`dept_id`) ON DELETE SET NULL ON UPDATE CASCADE) Hello guys i hope you're doing well. i have project for a clinic for company .i have created a registering form for patients that are employees too in the company and one of the indexes there are a department index to choose the department of the employees. when i select the department and submit the form this error comes to me everytime and i tried to fix it but it doesn't work. the tables are : table name : patients. structure : id , patient_name, company_type, date , age , phone_number, gender , job_num , national_num , nationality, job_id (the foregin key for jobs table on delete set null on update cascade) , dept_id ((the foregin key for departments table on delete set null on update cascade), illness_id (the foregin key for illnesses table on delete set null on update cascade). table : departments structure : dept_id , dept_name i use phpmyadmin , mariadb i make sure the data i insert is exist but still this error became. if someone can help me i'll be appreciated a lot.

5th Mar 2025, 10:12 AM
Nini
Nini - avatar
2 Answers
0
I think: on delete set Null can cause a problem. why? because it's an ID field which should be NOT NULL AUTO_INCREMENT
5th Mar 2025, 8:23 PM
Mihaly Nyilas
Mihaly Nyilas - avatar
0
but it works with other tables . what do you think the suitable constraint for the table?
5th Mar 2025, 10:18 PM
Nini
Nini - avatar