(doctrine2 + symfony2) 级联删除:违反完整性约束 1451 [英] (doctrine2 + symfony2) cascading remove : integrity constraint violation 1451
问题描述
首先,抱歉我的英语不好...
First, sorry for my poor English...
我有四个实体:用户、应用程序、捆绑包和;实体.这是它们的关系(使用级联持久化和删除,见下面的代码):
I got four entities : User, Application, Bundle & Entity. Here are their relations (with cascading persist & remove, see code below) :
- 用户1-n应用
- 应用 1-n 包
- 捆绑1-n实体
- User 1-n Application
- Application 1-n Bundle
- Bundle 1-n Entity
一切正常.但是一个用户可以有两个他的默认实体,我需要直接访问它们.
It's working fine. But an User can have two of his entities as default, and I need to access them directly.
所以我在 User 上添加了两个字段,entity1 &entity2,具有 1-1 关系.现在我的应用程序崩溃了:
So I add on User two fields, entity1 & entity2, with a 1-1 relation. And now my app crashes :
An exception occurred while executing 'DELETE FROM bundle WHERE id = ?' with params {"1":13}:
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`misc`.`entity`, CONSTRAINT `FK_E284468F1FAD9D3` FOREIGN KEY (`bundle_id`) REFERENCES `bundle` (`id`))
我尝试了几种方法,包括在 这篇文章中建立的那些,但我无法修复它.
I tried several things, including those founded in this post, but I wasn't able to fix it.
欢迎任何帮助,提前致谢.
Any help be welcome, thanks in advance.
我需要指出用户->实体关系是可选的:用户的 entity1 &entity2 可以为空.即使它们都为空也会发生错误.
EDIT : I need to point out that User->Entity relations are optionnal : User's entity1 & entity2 can be null. The error happens even if they are null both.
这是我的实体定义:
# User :
/**
* @ORMOneToMany(targetEntity="sfCommandsContentBundleEntityApplication", mappedBy="user", cascade={"remove"}, orphanRemoval=true)
* @ORMOrderBy({"name" = "ASC"})
*/
protected $applications;
/**
* @ORMOneToOne(targetEntity="sfCommandsContentBundleEntityEntity")
* @ORMJoinColumn(name="entity1_id", referencedColumnName="id")
*/
private $entity1;
/**
* @ORMOneToOne(targetEntity="sfCommandsContentBundleEntityEntity")
* @ORMJoinColumn(name="entity2_id", referencedColumnName="id")
*/
private $entity2;
#Application :
/**
* @ORMOneToMany(targetEntity="sfCommandsContentBundleEntityBundle", mappedBy="application", cascade={"remove"}, orphanRemoval=true)
* @ORMOrderBy({"name" = "ASC"})
*/
protected $bundles;
/**
* @ORMManyToOne(targetEntity="sfCommandsUserBundleEntityUser", inversedBy="applications", cascade={"persist"})
* @ORMJoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
#Bundle :
/**
* @ORMManyToOne(targetEntity="sfCommandsContentBundleEntityApplication", inversedBy="bundles", cascade={"persist"})
* @ORMJoinColumn(name="application_id", referencedColumnName="id")
*/
protected $application;
/**
* @ORMOneToMany(targetEntity="sfCommandsContentBundleEntityEntity", mappedBy="bundle", cascade={"remove"}, orphanRemoval=true)
* @ORMOrderBy({"name" = "ASC"})
*/
protected $entitys;
#Entity :
/**
* @ORMManyToOne(targetEntity="sfCommandsContentBundleEntityBundle", inversedBy="entitys", cascade={"persist"})
* @ORMJoinColumn(name="bundle_id", referencedColumnName="id")
*/
protected $bundle;
推荐答案
所以,感谢 这个法语论坛,我解决了这个问题.
So, thanks to this French forum, I fixed the problem.
我需要添加 nullable=true &onDelete="SET NULL" 在@ORMJoinColumn
I needed to add nullable=true & onDelete="SET NULL" in @ORMJoinColumn
这是可行的配置,也许它会帮助某人:
Here is the workable configuration, maybe it will help someone :
#User.
/**
* @ORMOneToMany(targetEntity="sfCommandsContentBundleEntityApplication", mappedBy="user", cascade={"remove"}, orphanRemoval=true)
* @ORMOrderBy({"name" = "ASC"})
*/
protected $applications;
/**
* @ORMOneToOne(targetEntity="sfCommandsContentBundleEntityEntity")
* @ORMJoinColumn(name="entity1_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
*/
private $entity1;
/**
* @ORMOneToOne(targetEntity="sfCommandsContentBundleEntityEntity")
* @ORMJoinColumn(name="entity2_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
*/
private $entity2;
#Application.
/**
* @ORMOneToMany(targetEntity="sfCommandsContentBundleEntityBundle", mappedBy="application", cascade={"remove"}, orphanRemoval=true)
* @ORMOrderBy({"name" = "ASC"})
*/
protected $bundles;
/**
* @ORMManyToOne(targetEntity="sfCommandsUserBundleEntityUser", inversedBy="applications", cascade={"persist"})
* @ORMJoinColumn(name="user_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
*/
protected $user;
#Bundle.
/**
* @ORMManyToOne(targetEntity="sfCommandsContentBundleEntityApplication", inversedBy="bundles", cascade={"persist"})
* @ORMJoinColumn(name="application_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
*/
protected $application;
/**
* @ORMOneToMany(targetEntity="sfCommandsContentBundleEntityEntity", mappedBy="bundle", cascade={"remove"}, orphanRemoval=true)
* @ORMOrderBy({"name" = "ASC"})
*/
protected $entitys;
#Entity.
/**
* @ORMManyToOne(targetEntity="sfCommandsContentBundleEntityBundle", inversedBy="entitys", cascade={"persist"})
* @ORMJoinColumn(name="bundle_id", referencedColumnName="id", nullable=true, onDelete="SET NULL")
*/
protected $bundle;
这篇关于(doctrine2 + symfony2) 级联删除:违反完整性约束 1451的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!