导入数据库时​​,外键约束因教义实体而失败

时间:2017-09-04 08:13:53

标签: mysql symfony doctrine-orm

我有一个大数据库要导入(大约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;

之前我导入相同的数据库时,一切都运行正常,我不认为我对结构进行了任何更改。

1 个答案:

答案 0 :(得分: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();
}

希望这有帮助,

Alexandru Cosoi