【发布时间】:2015-11-19 14:18:09
【问题描述】:
在我执行 Symfony2 Book 中解释的命令后:
php app/console doctrine:mapping:import --force AcmeBlogBundle xml
php app/console doctrine:mapping:convert annotation ./src
php app/console doctrine:generate:entities AcmeBlogBundle
它正确地创建了 XML 文件,正确地生成了实体。
但是当我跑步时:
php app/console doctrine:schema:validate
上面写着:
[映射] OK - 映射文件正确。
[Database] FAIL - 数据库架构与当前映射文件不同步。
好的,所以我按照我运行的教程进行操作:
php app/console doctrine:schema:update --dump-sql
在这里我可以看到有很多 ALTER TABLE 命令。像这样:
ALTER TABLE ea_restaurant_tag DROP FOREIGN KEY ea_restaurant_tag_ibfk_1;
ALTER TABLE ea_restaurant_tag DROP FOREIGN KEY ea_restaurant_tag_ibfk_2;
ALTER TABLE ea_restaurant_tag CHANGE id id INT AUTO_INCREMENT NOT NULL, CHANGE restaurant_id restaurant_id INT DEFAULT NULL, CHANGE login_id login_id INT DEFAULT NULL;
ALTER TABLE ea_restaurant_tag ADD CONSTRAINT FK_10C17C7E5CB2E05D FOREIGN KEY (login_id) REFERENCES ea_login (id);
ALTER TABLE ea_restaurant_tag ADD CONSTRAINT FK_10C17C7EB1E7706E FOREIGN KEY (restaurant_id) REFERENCES ea_restaurant (id);
更新
这是 restaurant_tag 表的 SQL“创建”命令:
CREATE TABLE IF NOT EXISTS `ea_restaurant_tag` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`restaurant_id` int(10) unsigned NOT NULL,
`login_id` int(10) unsigned DEFAULT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `login_id` (`login_id`),
KEY `restaurant_id` (`restaurant_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
ALTER TABLE `ea_restaurant_tag`
ADD CONSTRAINT `ea_restaurant_tag_ibfk_1` FOREIGN KEY (`restaurant_id`) REFERENCES `ea_restaurant` (`id`) ON DELETE CASCADE,
ADD CONSTRAINT `ea_restaurant_tag_ibfk_2` FOREIGN KEY (`login_id`) REFERENCES `ea_login` (`id`) ON DELETE CASCADE;
Doctribne 生成的 XML
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine- mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
<entity name="AppBundle\Entity\EaRestaurantTag" table="ea_restaurant_tag">
<indexes>
<index name="login_id" columns="login_id"/>
<index name="restaurant_id" columns="restaurant_id"/>
</indexes>
<id name="id" type="integer" column="id">
<generator strategy="IDENTITY"/>
</id>
<field name="date" type="datetime" column="date" nullable="false">
<options>
<option name="default">CURRENT_TIMESTAMP</option>
</options>
</field>
<many-to-one field="login" target-entity="EaLogin" fetch="LAZY">
<join-columns>
<join-column name="login_id" referenced-column-name="id"/>
</join-columns>
</many-to-one>
<many-to-one field="restaurant" target-entity="EaRestaurant" fetch="LAZY">
<join-columns>
<join-column name="restaurant_id" referenced-column-name="id"/>
</join-columns>
</many-to-one>
它试图删除当前密钥并重新创建其他密钥非常奇怪。 为什么会这样?
数据库是 MySQL,所有的表都是 InnoDB。
【问题讨论】:
-
请粘贴来自 MySQL 的“create table”命令和生成的 XML。谢谢
-
在问题中添加。谢谢。
-
尝试将
onDelete="CASCADE"选项添加到 Doctrine 试图重新创建的连接列。这可能是 Doctrine 无法识别您当前的外键的原因。 -
没有任何变化。但是上面的问题发生在每个数据库表中。这太不可思议了。在每个外键中,它都说要删除它并重新创建。这不正常。
标签: mysql symfony doctrine-orm