문제

When using Doctrine ORM in Symfony2, I have the following tables generated from three different entities, of which accessory has two foreign key constraints (marked A and B below).

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    |                |
+-------------------+--------------+------+-----+---------+----------------+

The foreign keys are mapped with annotations:

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;

but upon running php app/console doctrine:schema:update --force I got this exception

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

So I ran 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);

What's the correct way to resolve this? Should I edit the tables manually or with Doctrine?

Based on what I've read about errno 150, the foreign column needs to be indexed, but can't Doctrine handle this automatically?

도움이 되었습니까?

해결책

One could add unique=true to both $nameCanonical and $titleCanonical properties or whichever columns are referenced by the foreign key. Then drop and run the schema update command to recreate the tables.

In the case of $titleCanonical

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

But ideally with Doctrine, the foreign keys should be referenced to the other table's primary key to make it valid.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top