laravel插入查询失败,但可在mysql中运行

时间:2018-10-25 21:53:02

标签: php mysql laravel insert

我无法弄清楚,希望有人知道我在做什么错。 我有三个表:

教学任务表

CREATE TABLE `teaching task` (
  `Task Code` varchar(10) NOT NULL,
  `Semester` varchar(45) NOT NULL,
  `Year` varchar(45) DEFAULT NULL,
  `Groups` int(11) DEFAULT NULL,
  `Tutors` int(11) DEFAULT NULL,
  `Description` varchar(500) DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`Task Code`,`Semester`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

模块表

CREATE TABLE `module` (
  `Code` varchar(10) NOT NULL,
  `Name` varchar(45) DEFAULT NULL,
  `Year` varchar(45) NOT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
   PRIMARY KEY (`Code`,`Year`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

数据透视表

CREATE TABLE `teaching task_involves_module` (
  `Teaching Task_Task Code` varchar(10) NOT NULL,
  `Teaching Task_Semester` varchar(45) NOT NULL,
  `Module_Code` varchar(10) NOT NULL,
  `Module_Year` varchar(45) NOT NULL,
  PRIMARY KEY (`Teaching Task_Task Code`,`Teaching Task_Semester`,`Module_Code`,`Module_Year`),
  KEY `fk_Teaching Task_involves_Module_Module1_idx` (`Module_Code`,`Module_Year`),
  CONSTRAINT `fk_Teaching Task_involves_Module_Module1` 
    FOREIGN KEY (`Module_Code`, `Module_Year`) 
        REFERENCES `module` (`Code`, `Year`) 
        ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `fk_Teaching Task_involves_Module_Teaching Task1` 
    FOREIGN KEY (`Teaching Task_Task Code`, `Teaching Task_Semester`) 
        REFERENCES `teaching task` (`Task Code`, `Semester`) 
        ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8

我正在尝试将一条记录添加到教学任务表中,然后在数据透视表中添加一个条目,以将其与模块相关联。

我正在做两个查询。 首先,我使用雄辩的方法将教学任务保存到Teaching_task表中,然后使用查询生成器将记录插入到数据透视表中。

$newttask = new Teaching_Task;

// composite primary key:
$newttask->{'Task Code'} = $req->{'Task Code'};
$newttask->{'Semester'} = $req->{'Semester'};

$newttask->{'Year'} = substr($req->{'Semester'}, 0, -1);
$newttask->{'Groups'} = $req->{'Groups'};
$newttask->{'Tutors'} = $req->{'Tutors'};
$newttask->{'Description'} = $req->{'Description'};

// save model
if ($newttask->save()) {

    $outcome = "";

    // insert into `teaching task_involves_module`
    $ttim = DB::insert("INSERT INTO  `teaching task_involves_module` 
            (`Teaching Task_Task Code`, `Teaching Task_Semester`, 
             `Module_Code`, `Module_Year`) VALUES ('?', '?', '?', '?')",
        [$newttask->{'Task Code'},
         $newttask->{'Semester'},
         $req->{'Module_Code'},
         $newttask->{'Year'}] );

} else {
    $outcome = "Not created";
}

第一个查询成功,但第二个失败:

  

SQLSTATE [23000]:违反完整性约束:1452无法添加或更新子行:外键约束失败(qedtest_dbteaching task_involves_module,CONSTRAINT fk_Teaching Task_involves_Module_Teaching Task1外键({{ 1}},Teaching Task_Task Code)参考文献Teaching Task_Semester教task_involves_module teachin) (SQL: INSERT INTO教Task_Task代码(教Task_Semester , Module_Code , Module_Year`)值(' C101-XX”,“ 2018191”,“ COMP101”,“ 201819”))

如果我在SQLyog中运行查询,它将运行正常。为什么在Laravel中不起作用?

0 个答案:

没有答案