symfony – Doctrine 2架构更新产生MySQL errno 150,外键约束

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

describe publication;
+-------------------+--------------+------+-----+---------+----------------+
| Field             | Type         | Null | Key | Default | Extra          |
+-------------------+--------------+------+-----+---------+----------------+
| id                | int(11)      | NO   | PRI | NULL    | auto_increment |
| kid               | varchar(10)  | NO   |     | NULL    |                |
| title             | varchar(255) | NO   |     | NULL    |                |
| title_canonical   | varchar(255) | NO   |     | NULL    |                | <- A
| created           | datetime     | NO   |     | NULL    |                |
| modified          | datetime     | NO   |     | NULL    |                |
+-------------------+--------------+------+-----+---------+----------------+

describe accessory;
+-------------------+--------------+------+-----+---------+----------------+
| Field             | Type         | Null | Key | Default | Extra          |
+-------------------+--------------+------+-----+---------+----------------+
| id                | int(11)      | NO   | PRI | NULL    | auto_increment |
| publication_title | varchar(255) | YES  |     | NULL    |                | <- A
| index_id          | int(11)      | NO   |     | NULL    |                |
| index_alias       | varchar(255) | NO   |     | NULL    |                |
| value             | longtext     | NO   |     | NULL    |                |
| attribute_name    | varchar(255) | YES  |     | NULL    |                | <- B
+-------------------+--------------+------+-----+---------+----------------+

describe attribute;
+-------------------+--------------+------+-----+---------+----------------+
| Field             | Type         | Null | Key | Default | Extra          |
+-------------------+--------------+------+-----+---------+----------------+
| id                | int(11)      | NO   | PRI | NULL    | auto_increment |
| name              | varchar(255) | NO   |     | NULL    |                |
| name_canonical    | varchar(255) | NO   |     | NULL    |                | <- B
| parameter         | varchar(16)  | NO   |     | NULL    |                |
+-------------------+--------------+------+-----+---------+----------------+

外键使用注释进行映射:

Publication.php

/**
 * @ORM\OneToMany(targetEntity="Accessory", mappedBy="publication")
 */
protected $accessories;

Accessory.php

/**
 * @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;

Attribute.php

/**
 * @ORM\OneToMany(targetEntity="Accessory", mappedBy="attribute")
 */
protected $accessories;

但运行php app / console doctrine: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 app / console doctrine:schema:update –dump-sql

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);

解决这个问题的正确方法是什么?我应该手动编辑表还是使用Doctrine编辑表?

基于我所读到的关于errno 150的内容,需要对外部列进行索引,但是Doctrine不能自动处理吗?

最佳答案
可以在$nameCanonical和$titleCanonical属性或外键引用的任何列中添加unique = true.然后删除并运行schema update命令以重新创建表.

在$titleCanonical的情况下

/**
 * @var string
 *
 * @ORM\Column(name="title_canonical", type="string", length=255, unique=true)
 */
private $titleCanonical;

但理想情况下,使用Doctrine时,外键应该引用另一个表的主键以使其有效.

转载注明原文:symfony – Doctrine 2架构更新产生MySQL errno 150,外键约束 - 代码日志