Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 Doctrine2 mapping from existing database (exception)

I've already got a MySQL database therefore I wish to create mapping meta data from the 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 haven't used customerId in any primary / composite key anywhere else in the database however I've used it as a foreign key several times.

However I do not know how having customerId in a composite key or another primary key could affect this.

like image 954
klj613 Avatar asked Aug 12 '11 19:08

klj613


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

like image 184
Szendvics Avatar answered Sep 30 '22 15:09

Szendvics


Another solution:

Drop all the foreign keys, Then it will work :) .

i know it is not recommended but it helped me. and generated Entities were working fine.

To drop all the foreign keys:

run this sql query -

MySQL

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;

PostgreSQL

SELECT concat('ALTER TABLE ', table_name, ' DROP CONSTRAINT IF EXISTS ', constraint_name, ';')
FROM information_schema.key_column_usage
WHERE constraint_schema = 'public' AND constraint_catalog = 'one' AND constraint_name LIKE '%_fkey';

and then run the resulted sql query again.

like image 32
vivex Avatar answered Sep 30 '22 16:09

vivex


i got same error. i generate entity from exists database.

php bin/console doctrine:mapping:import App\\Entity annotation --path=src/Entity --force --verbose --no-interaction

error message is 'Property "vacancy" in "User" was already declared, but it must be declared'

I found the cause of the error when debugging the doctrine

if there is relation between one table and more than one table with manytomany and Column name is same on relate table there is relation User table and vacancy_a,vacancy_b on vacancy_id column name.

select C.COLUMN_NAME,C.TABLE_NAME,K.* from information_schema.KEY_COLUMN_USAGE K inner join information_schema.`COLUMNS` C on (C.TABLE_NAME = K.TABLE_NAME and C.TABLE_SCHEMA = K.TABLE_SCHEMA) where K.TABLE_SCHEMA='schema_name' and K.REFERENCED_TABLE_NAME='**user**' and C.COLUMN_NAME='**vacancy**_id' order by C.COLUMN_NAME

Result of Query is relate column and table.

Solution : Rename to column name

ALTER TABLE vacancy_a CHANGE vacany_id vacancy_a_id int(11);
like image 24
ureyni Avatar answered Sep 30 '22 15:09

ureyni