2017-09-04 171 views
0

我有一個大數據庫導入(大約350 Mb)。當試圖導入該數據庫使用命令行的MySQL,我有這樣的錯誤:導入數據庫時​​導致外鍵約束失敗,導出實體失敗

ERROR 1452 (23000) at line 5118296: Cannot add or update a child row: a foreign key 
constraint fails (`tutotour_tharmobeta1`.`#sql-2039_195b4`, CONSTRAINT 
`FK_D367B678BF396750` FOREIGN KEY (`id`) REFERENCES `reponse` (`id`) ON DELETE CASCADE) 

在此行中的SQL文件,我有:

-- 
-- Contraintes pour la table `reponse` 
-- 
ALTER TABLE `reponse` 
ADD CONSTRAINT `FK_5FB6DEC75DDDBC71` FOREIGN KEY (`error`) REFERENCES 
`errorcsv` (`id`), 
ADD CONSTRAINT `FK_5FB6DEC78D93D649` FOREIGN KEY (`user`) REFERENCES `user` 
(`id`), 
ADD CONSTRAINT `FK_5FB6DEC7D6ADE47F` FOREIGN KEY (`epreuve`) REFERENCES 
`banque_epreuve_temporaire` (`id`), 
ADD CONSTRAINT `FK_5FB6DEC7DCC6487D` FOREIGN KEY (`passage_id`) REFERENCES 
`passer_colle` (`id`); 

-- 
-- Contraintes pour la table `reponse_qc` 
-- 
ALTER TABLE `reponse_qc` 
ADD CONSTRAINT `FK_D367B678B6F7494E` FOREIGN KEY (`question`) REFERENCES 
`qc` (`id`), 
ADD CONSTRAINT `FK_D367B678BF396750` FOREIGN KEY (`id`) REFERENCES `reponse` 
(`id`) ON DELETE CASCADE; 

-- 
-- Contraintes pour la table `reponse_qr` 
-- 
ALTER TABLE `reponse_qr` 
ADD CONSTRAINT `FK_B9D7968A8D5FE0A2` FOREIGN KEY (`idCorrecteur`) REFERENCES 
`tuteur` (`id`), 
ADD CONSTRAINT `FK_B9D7968ABF396750` FOREIGN KEY (`id`) REFERENCES `reponse` 
(`id`) ON DELETE CASCADE, 
ADD CONSTRAINT `reponse_qr_ibfk_2` FOREIGN KEY (`idColle`) REFERENCES 
`colle_qr` (`id`); 

如下我的實體定義:

效應初探實體:

/** 
* Réponse 
* 
* @ORM\Table("reponse") 
* @ORM\Entity(repositoryClass="PACES\ColleBundle\Repository\ReponseRepository") 
* @ORM\InheritanceType("JOINED") 
* @ORM\DiscriminatorColumn(name="discr", type="string") 
* @ORM\DiscriminatorMap({"reponse"="Reponse", "reponseQC"="ReponseQC", 
"reponseQR"="ReponseQR"}) 
*/ 
class Reponse 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

/** 
* @ORM\ManyToOne(targetEntity="\PACES\UserBundle\Entity\User", inversedBy="reponses", cascade={"persist"}) 
* @ORM\JoinColumn(name="user", referencedColumnName="id", nullable=true) 
*/ 
private $user; 


/** 
* @ORM\ManyToOne(targetEntity="\PACES\ColleBundle\Entity\ErrorCSV", inversedBy="reponses", cascade={"persist"}) 
* @ORM\JoinColumn(name="error", referencedColumnName="id") 
*/ 
private $error; 

/** 
* @var boolean 
* @ORM\Column(name="archive" , type="boolean" , nullable=false, options={"default" = false}) 
*/ 
private $archive; 

/** 
* @ORM\Column(name="banque", type="boolean", options={"default" = false}) 
*/ 
protected $banque; 

/** 
* @var \DateTime 
* 
* @ORM\Column(name="date", type="datetime", nullable=true) 
*/ 
private $date; 

/** 
* @ORM\ManyToOne(targetEntity="PACES\ColleBundle\Entity\PasserColle", inversedBy="reponses", cascade={"persist"}) 
*/ 
private $passage; 

/** 
* @ORM\ManyToOne(targetEntity="PACES\BanqueBundle\Entity\BanqueEpreuveTemporaire", inversedBy="reponses", cascade={"persist"}) 
* @ORM\JoinColumn(name="epreuve", referencedColumnName="id") 
*/ 
private $epreuveTemporaire; 

ReponseQC:

/** 
* ReponseQC 
* 
* @ORM\Table("reponse_qc") 
* @ORM\Entity(repositoryClass="PACES\ColleBundle\Repository\ReponseQCRepository") 
*/ 
class ReponseQC extends Reponse 
{ 
/** 
* @ORM\ManyToOne(targetEntity="QC", inversedBy="reponses", cascade={"persist"}) 
* @ORM\JoinColumn(name="question", referencedColumnName="id") 
*/ 
private $question; 

當我導入相同的數據庫之前,一切正常,我不認爲我對結構做了任何更改。

回答

0

嘗試一些類似這樣的

$connection = $em->getConnection(); 
$connection->beginTransaction(); 
try { 
    $connection->query('SET FOREIGN_KEY_CHECKS=0'); 
    /* run your query here */ 
    $connection->query('SET FOREIGN_KEY_CHECKS=1'); 
    $connection->commit(); 
} 
catch (\Exception $e) { 
    $connection->rollback(); 
} 

希望這有助於

亞歷Cosoi

+0

您可能要解釋你的答案了一下,你會受到downvotes發生前。 – svgrafov

+0

這就是我首先想到的,但我認爲這是一種解決方法。它並沒有真正解決問題...我忘了說我直接用命令行mysql導入數據庫 –

+0

@svgrafov您嘗試運行的查詢被外鍵約束阻止。我不知道這個約束是什麼,因爲我沒有完整的數據庫鏡像,但是如果你確定你的查詢沒問題,你可以告訴mysql停止檢查這種類型的約束,直到你運行你的查詢,通過調用$ connection-> query('SET FOREIGN_KEY_CHECKS = 0');在運行查詢之前。在$ connection-> query('SET FOREIGN_KEY_CHECKS = 1')之後再打開該檢查。如果你認爲這沒有什麼幫助,而我試圖幫助你需要一個投票,那麼你可以隨意做到這一點。 –