Mapping Symfony2 Doctrine2 from an existing database (exception)

I already have a MySQL database, so I want to create mapping metadata from an existing database .

 php app/console doctrine:mapping:convert xml ./src/MainBundle/Resources/config/doctrine/metadata/orm --from-database --force 

However, I got the following exception

 [Doctrine\ORM\Mapping\MappingException] Property "customerid" in "Accountcustomer" was already declared, but it must be declared only once 

I did not use customerId in any primary / compound key anywhere in the database, however I used it as a foreign key several times.

However, I do not know how to have customerId in a composite key or another primary key can affect this.

+4
source share
3 answers

Unfortunately, Doctrine 2.0 does not support primary keys as foreign keys ... Check the link: http://www.doctrine-project.org/docs/orm/2.0/en/reference/limitations-and-known-issues.html

+6
source

Another solution:

Drop all foreign keys, then it will work :).

I know this is not recommended, but it helped me. and the generated objects worked fine.

To delete all foreign keys:

run this sql query -

 SELECT concat('ALTER TABLE ', TABLE_NAME, ' DROP FOREIGN KEY ', CONSTRAINT_NAME, ';') FROM information_schema.key_column_usage WHERE CONSTRAINT_SCHEMA = 'db_name' AND referenced_table_name IS NOT NULL; 

and then run the resulting SQL query again.

+3
source

I got the same error and I noticed that I had a double key (restriction) for single relations in db. Removing it, everything worked fine.

0
source

All Articles