5

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.

1
  • 1
    This error was is stating that there are duplicate (or more) Foreign keys on the same table. Keeping just 1 FK fixes the problem. Commented Apr 4, 2016 at 11:38

4 Answers 4

6

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

Sign up to request clarification or add additional context in comments.

2 Comments

Ok, but I have the exact same error with doctrine:schema:create, on an empty database...
This still does not work on Doctrine 2.1 (the reverse engineering part)
4

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.

2 Comments

Not a good solution to remove all foreign keys. The error was obviously stating that there were duplicate (or more) Foreign keys on the same table. Keeping just 1 FK fixes the problem.
I would mention, that database backup procedure reprecaution might be necessary. +1 Helped.
2

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.