klj613
klj613

Reputation: 177

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.

Upvotes: 5

Views: 10641

Answers (4)

vivex
vivex

Reputation: 2515

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.

Upvotes: 4

ureyni
ureyni

Reputation: 71

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

Upvotes: 2

dom
dom

Reputation: 211

I got the same error and i noticed i had some double key (constraint) for single relationships in the db. Removing it, everythings worked fine.

Upvotes: 0

Szendvics
Szendvics

Reputation: 92

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

Upvotes: 6

Related Questions