Symfony2,DoctrineFixturesBundle,由于外键约束,无法加载固定装置 [英] Symfony2, DoctrineFixturesBundle, can't load fixtures due to foreign key constraint

查看:117
本文介绍了Symfony2,DoctrineFixturesBundle,由于外键约束,无法加载固定装置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个公司实体,每个公司都有一个分层树结构的另一家母公司。

I have a Company entity where each company has another parent company in a hierarchical tree structure.

一切都在应用程序中运行正常,所以我确定我的实体类是正确的。

Everything works fine in the application so I'm sure my Entity classes are correct.

问题是,如果数据库中已经有内容,然后执行

The problem is, if there is already content in the database then doing

doctrine:fixtures:load

给出了这个错误:

[PDOException]                                                                                                                                                                                                                                              
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails

我相当确定问题是load:fixtures必须截断表,但它不能得到这个错误。

Im fairly sure the problem is that the load:fixtures has to truncate the table, but it cant without getting this error.

我不知道该怎么办解决这个问题,而不是在Doctrine中进行某些操作,以在清除之前禁用关键约束。不是真正的长期解决方案。

Im not sure how to resolve this without hacking something in to Doctrine to disable key constraints before the purge. Not really a long term solution.

数据结构中的其他关系不会导致问题,因为教义似乎以正确的顺序清除以避免问题,但是

The other relationships in the data structure dont cause a problem as doctrine seems to purge in the right order to avoid the problems, but with the Company table being self referencing it falls over.

Heres my entity。

Heres my entity.

class Company
{
/**
 * @var integer $id
 *
 * @ORM\Id
 * @ORM\Column(name="id", type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
* @var string $name
* @ORM\Column(type="string", length=100)
*/    
protected $name;

/**
 * @ORM\ManyToOne(targetEntity="Company", inversedBy="children")
 * @ORM\JoinColumn(name="parent_id", referencedColumnName="id")
 */
protected $parent;

/* other properties here..... */

}

我使用Symfony 2.0.7和最新的deps和MySQL 5.5

Im using Symfony 2.0.7 and the latest deps, and MySQL 5.5

推荐答案

我只需要正确设置OnDelete的行为。

I just needed to properly set the behaviour of OnDelete.

默认为RESTRICT,它解释了抛出的错误。明确地将其设置为CASCADE或SET NULL可以让教条清空表格,而不会出现错误。

It defaults to RESTRICT, which explains the error being thrown. Explicitly setting it to CASCADE or SET NULL allows doctrine to empty the table with no errors.

在我的情况下,我不想删除父项导致删除孩子,所以我使用SET NULL,这样才能将关系改为代替。

In my case I didnt want removing a parent to cause the deletion of the children, so I used SET NULL so that the relationships would just be removed instead.

/**
 * @ORM\ManyToOne(targetEntity="Company", inversedBy="children")
 * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="set null")
 */
protected $parent;

这篇关于Symfony2,DoctrineFixturesBundle,由于外键约束,无法加载固定装置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆