doctrine schema更新总是要求约束和索引

时间:2014-04-21 16:42:59

标签: mysql symfony ubuntu doctrine-orm phpmyadmin

我在ubuntu 12.04上使用symfony, 当我从命令行操作doctrine架构更新时, 转储总是打印出他必须做出这样的事情:

ALTER TABLE melu_numeros ADD CONSTRAINT FK_8062E62AFB539063 FOREIGN KEY (id_fan) REFERENCES melu_fanzine (id_fan);
ALTER TABLE Image ADD CONSTRAINT FK_4FC2B5BABEDD0E7 FOREIGN KEY (id_num_fan) REFERENCES melu_numeros (id_num_fan);
CREATE INDEX IDX_4FC2B5BABEDD0E7 ON Image (id_num_fan);
DROP INDEX IDX_7B0247F15D172A78 ON melu_categories;
ALTER TABLE melu_categories ADD id_num_fan INT NOT NULL, DROP numero_id;
ALTER TABLE melu_categories ADD CONSTRAINT FK_7B0247F1ABEDD0E7 FOREIGN KEY (id_num_fan) REFERENCES melu_numeros (id_num_fan);
CREATE INDEX IDX_7B0247F1ABEDD0E7 ON melu_categories (id_num_fan);
ALTER TABLE melu_fichiers ADD CONSTRAINT FK_E2A048FCD6E65D60 FOREIGN KEY (id_agen) REFERENCES melu_agenda (id_agen);
ALTER TABLE melu_fanzine ADD CONSTRAINT FK_99AC1D58A656D38D FOREIGN KEY (id_asso) REFERENCES melu_association (id_asso);
ALTER TABLE melu_tarifs ADD CONSTRAINT FK_409B4C8D6E65D60 FOREIGN KEY (id_agen) REFERENCES melu_agenda (id_agen);
ALTER TABLE melu_newsletter CHANGE email email VARCHAR(50) NOT NULL;
ALTER TABLE melu_commentaire ADD CONSTRAINT FK_EE62B25DABEDD0E7 FOREIGN KEY (id_num_fan) REFERENCES melu_numeros (id_num_fan);
ALTER TABLE melu_contenus DROP INDEX FK_D9DCDC6130453DE8, ADD UNIQUE INDEX UNIQ_D9DCDC6130453DE8 (type_cont);
ALTER TABLE melu_contenus ADD CONSTRAINT FK_D9DCDC6130453DE8 FOREIGN KEY (type_cont) REFERENCES melu_types (id_types);
CREATE UNIQUE INDEX id_cont ON melu_contenus (id_cont);
ALTER TABLE melu_types ADD CONSTRAINT FK_8AB9AD2AB7B5C800 FOREIGN KEY (id_types) REFERENCES melu_contenus (type_cont);
ALTER TABLE melu_photos ADD CONSTRAINT FK_F5C79087D6E65D60 FOREIGN KEY (id_agen) REFERENCES melu_agenda (id_agen);
ALTER TABLE melu_jour ADD CONSTRAINT FK_85C738A2D6E65D60 FOREIGN KEY (id_agen) REFERENCES melu_agenda (id_agen);
DROP INDEX IDX_6C66C2856D2F0F48 ON melu_util_droit;
ALTER TABLE melu_util_droit DROP useur_id;
ALTER TABLE melu_util_droit ADD CONSTRAINT FK_6C66C28550EAE44 FOREIGN KEY (id_utilisateur) REFERENCES melu_utilisateurs (id_util);

当我强制更新时,我有这个错误:

[Doctrine\DBAL\DBALException]                                                                                                   
An exception occurred while executing 'ALTER TABLE Image ADD CONSTRAINT FK_4FC2B5BABEDD0E7 FOREIGN KEY (id_num_fan) REFERENCES  melu_numeros (id_num_fan)':
SQLSTATE[HY000]: General error: 1005 Can't create table 'meluzineorg1.#sql-5cf_438' (errno: 150)

2引发Image和melu_numero引发错误 http://pastie.org/9100202

我怎么能让这一切都运作良好?

3 个答案:

答案 0 :(得分:1)

解决了,我从一大堆 myisam 表中继承了数据表。

Docisine似乎在myisam引擎中存在外键问题,所以我在 innoDb 中转换了所有内容,现在没有更多关于架构更新的笑话了一半:)

谢谢大家

答案 1 :(得分:0)

错误150表示你的主键有问题,一个例子可能是你改变了一个表中的键,你没有改变另一个表上的约束

尝试阅读这个答案,因为它的相似之处(如果不相同) MySQL: Can't create table (errno: 150)

答案 2 :(得分:0)

问题出在img

的映射中
/**
 * @var arrayCollection
 * @ORM\OneToMany(targetEntity="Tykayn\MeluzineBundle\Entity\Image", mappedBy="numero", cascade={"persist", "merge"})
 * @ORM\JoinColumn(name="id_num_fan", referencedColumnName="id_num_fan")
 */
private $img;

如您所见,您将refrencedColumnName设置为id_num_fan,但在您在Image实体中的映射中,您没有将id_num_fan定义为在顶层实体中使用外键的关键字
您可以更改Image实体中的映射,如下所示,并删除$id属性(如果未在其他实体中用作外键),或者您可以将顶部映射更改为* @ORM\JoinColumn(name="id", referencedColumnName="id"),因为id也是关键。

/**
 * @var integer 
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * @ORM\Column(name="id_num_fan", type="integer", nullable=false )
 */
private $idNumFan;