Solution Doctrine Error: The table with name 'yourtable.tablename' already exists

If you're working on some PHP project based on Symfony or that uses the Doctrine ORM to build and you're unable to create the tables in MySQL using the following command:

php bin/console doctrine:schema:update --force

You probably are freaking out in this moment because you don't know how to solve it isn't? Well, in this article we'll provide you some tips that may help you to ride out of this problem.

Possible cause #1

In most of the cases and probably for your project too, this error is caused because you have 2 entities and orm files for the same table in your project and they're probably located in different bundles, for example:

/AppBundle/Entity/Table.php
/AppBundle/Resources/config/doctrine/Table.orm.yml

# Somewhere else in other bundle of your app

/OtherBundle/Entity/Table.php
/OtherBundle/Resources/config/doctrine/Table.orm.yml

Note that according to the way you work, you may not use the mapping with orm.yml file but with annotations in your entities instead. So all you need to to is to track for the duplicated entity and remove them (you can change the name) and finally try to update your schema with the command again. Obviusly because doctrine won't create the same table twice, this exception will be thrown. 

In this case the reason is because the usage of reverse engineering with doctrine, which is obviously not wrong, but in this case is creating files that you probably didn't knew were created. The identification of this problem is even harder if you don't use control version on your projects.

Possible cause #2

If you have checked that indeed your table isn't being created twice because there are 2 entities or orm files of a table, you will need to check the One-To-Many, Unidirectional with Join Table relation like annotations in your project because you may probably modified manually one of those files and you did it as you can see wrong.

As this problem depends totally on the way you work with your project, we can't help you with it, so you will need to read and analyze carefully the structure of your database and rewrite the relations correctly. If you still stucked with the problem, then we recommend you to create a question on Stack Overflow and provide all the information of your entities, orm files and maybe someone will be able to help you with this.

May the force be with you !

Become a more social person