在 Symfony2 中使用 Doctrine ORM 时,我从三个不同的实体生成了以下表格,其中accessory
有两个外键约束(下面标记为 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 | |
+-------------------+--------------+------+-----+---------+----------------+
外键使用注释映射:
发布.php
/**
* @ORM\OneToMany(targetEntity="Accessory", mappedBy="publication")
*/
protected $accessories;
附件.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;
属性.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 不能自动处理这个吗?