Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

symfony - (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) :

  • 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.

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.

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.

Here are my entities definitions :

# 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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So, thanks to this French forum, I fixed the problem.

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...