MySQL错误1452 - 无法添加或更新?

时间:2011-07-06 11:50:06

标签: mysql

这是我的数据库:

http://i.imgur.com/D1M4J.png

我试图在将数据插入到其周围的所有表中之后将数据插入到我的主链接表过程中。我已经检查了插入的数据,看起来没问题,我也检查了我的关系。

$proceduredata = array 
            (
                'patient_id' => $patientfk,
                'name_id' => $procedurenamefk,
                'department_id' => $departmentfk,
                'dosage_id' => $dosagefk,
                'edocument' => NULL, //not implemented yet
                'user_id' => $this->session->userdata('userID'),
                'duration' => NULL, //not implemented yet
                'submitted' => date('d-m-Y H:i:s', now()),
                'comment' => NULL, //to be implemented

            );

            $insertprocedure = $this->db->insert('procedure', $proceduredata); //inserts into 'procedure' table with $proceduredata array
            $procedurefk = $this->db->insert_id(); //gets the primary key of the row inserted

错误消息:

  

发生数据库错误

     

错误号码:1452

     

无法添加或更新子行:外键约束失败(midas1procedure,CONSTRAINT belongs to a FOREIGN KEY(department_id)参考departmentdepartment_id))

     

INSERT INTO procedurepatient_idname_iddepartment_iddosage_idedocumentuser_id,{{1 {},durationsubmitted)VALUES(17,7,0,NULL,NULL,'2',NULL,'06 -07-2011 13:41:01',NULL)

     

文件名:C:\ xampp \ htdocs \ midas \ system \ database \ DB_driver.php

     

行号:330

感谢您的时间

1 个答案:

答案 0 :(得分:0)

$departmentfk似乎是0,但根据您在MySQL中的约束,需要是department表中的ID。 MySQL检查department表并找不到相应的ID,这就是它失败的原因。