在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 | |
+-------------------+--------------+------+-----+---------+----------------+
使用注释映射外键:
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不能自动处理吗?
答案 0 :(得分:4)
可以将unique=true
添加到$ nameCanonical和$ titleCanonical属性或外键引用的任何列。然后删除并运行schema update命令以重新创建表。
在$ titleCanonical
的情况下/**
* @var string
*
* @ORM\Column(name="title_canonical", type="string", length=255, unique=true)
*/
private $titleCanonical;
但理想情况下,使用Doctrine,外键应该引用另一个表的主键以使其有效。