Symfony 条令2模式更新产生MySQL errno 150,外键约束

Symfony 条令2模式更新产生MySQL errno 150,外键约束,symfony,doctrine-orm,foreign-keys,mysql-error-1005,Symfony,Doctrine Orm,Foreign Keys,Mysql Error 1005,在Symfony2中使用ORM时,我从三个不同的实体生成了以下表格,其中附件有两个外键约束(下面标记A和B) 附件.php /** * @ORM\OneToMany(targetEntity="Accessory", mappedBy="publication") */ protected $accessories; /** * @ORM\ManyToOne(targetEntity="Publication", inversedBy="accessories") * @ORM\Joi

在Symfony2中使用ORM时,我从三个不同的实体生成了以下表格,其中
附件
有两个外键约束(下面标记A和B)

附件.php

/**
 * @ORM\OneToMany(targetEntity="Accessory", mappedBy="publication")
 */
protected $accessories;
/**
 * @ORM\ManyToOne(targetEntity="Publication", inversedBy="accessories")
 * @ORM\JoinColumn(name="publication_title", referencedColumnName="title_canonical")
 */
protected $publication;


/**
 * @ORM\ManyToOne(targetEntity="Attribute", inversedBy="accessories")
 * @ORM\JoinColumn(name="attribute_name", referencedColumnName="name_canonical")
 */
protected $attribute;
/**
 * @ORM\OneToMany(targetEntity="Accessory", mappedBy="attribute")
 */
protected $accessories;
Attribute.php

/**
 * @ORM\OneToMany(targetEntity="Accessory", mappedBy="publication")
 */
protected $accessories;
/**
 * @ORM\ManyToOne(targetEntity="Publication", inversedBy="accessories")
 * @ORM\JoinColumn(name="publication_title", referencedColumnName="title_canonical")
 */
protected $publication;


/**
 * @ORM\ManyToOne(targetEntity="Attribute", inversedBy="accessories")
 * @ORM\JoinColumn(name="attribute_name", referencedColumnName="name_canonical")
 */
protected $attribute;
/**
 * @ORM\OneToMany(targetEntity="Accessory", mappedBy="attribute")
 */
protected $accessories;
但是在运行
php应用程序/控制台原则:schema:update--force时,我得到了这个异常

[Doctrine\DBAL\DBALException]                                                                                                                                               
An exception occurred while executing 'ALTER TABLE accessory ADD CONSTRAINT FK_A1B1251CCEE83EE7 FOREIGN KEY (publication_title) REFERENCES publication (title_canonical)':  

SQLSTATE[HY000]: General error: 1005 Can't create table 'publicationsapp.#sql-2a3c_2828' (errno: 150)
所以我运行了
php应用程序/控制台原则:schema:update--dumpsql

ALTER TABLE accessory ADD CONSTRAINT FK_A1B1251CCEE83EE7 FOREIGN KEY (publication_title) REFERENCES publication (title_canonical);
ALTER TABLE accessory ADD CONSTRAINT FK_A1B1251C5CBDA8E FOREIGN KEY (attribute_name) REFERENCES attribute (name_canonical);
CREATE INDEX IDX_A1B1251CCEE83EE7 ON accessory (publication_title);
CREATE INDEX IDX_A1B1251C5CBDA8E ON accessory (attribute_name);
解决这个问题的正确方法是什么?我应该手动编辑表格还是使用条令编辑表格


根据我所读到的关于errno 150的内容,外键列需要被索引,但是不能自动处理吗?

可以将
unique=true
添加到$nameCononical和$titlecononical属性中,或者外键引用的任何列中。然后删除并运行schema update命令以重新创建表

就$titleCanonical而言

/**
 * @var string
 *
 * @ORM\Column(name="title_canonical", type="string", length=255, unique=true)
 */
private $titleCanonical;
但理想情况下,对于条令,外键应该引用到另一个表的主键以使其有效