Luca Coppola
Luca Coppola

Reputation: 53

Symfony2 doctrine reverse engineering issue

After I executed the commands explained in the 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

It created correctly the XML files, generated correctly the entities.

But when I run:

php app/console doctrine:schema:validate

It says:

[Mapping] OK - The mapping files are correct.

[Database] FAIL - The database schema is not in sync with the current mapping file.

Ok so I following the tutorials I run:

php app/console doctrine:schema:update --dump-sql

And here I can see that there are a lot of ALTER TABLE commands. Like this:

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

UPDATED

This is the SQL "create" command for the restaurant_tag table:

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;

XML generated by Doctribne

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

It's very strange it tries to delete the current keys and recreate others. Why this happens ?

The database is MySQL and all tables are InnoDB.

Upvotes: 0

Views: 421

Answers (1)

Vasily L
Vasily L

Reputation: 146

Doctrine import tool doesn't support unsigned primary keys. You probably has to define it manually by setting columnDefinition="INTEGER UNSIGNED" in JoinColumn or extending EntityGenerator class.

Upvotes: 0

Related Questions