Symfony2, Doctrine2 - force update - table already exists on many-to-many relation

Looks like you already have table named "categories" in that database. Remove this line @ORM\JoinTable(name="categories") and try without it.

P.S. "Categories" is really a strange name for join table. You should probably follow some conventions and let doctrine name it. Common names for join tables are category_task or category2task as they are more self-explanatory. Nothing that important, just trying to suggest what I consider good practice.


The thing is that doctrine doesn't understand how your existing table should be used. But you can give him some help.

You have two options :

  • You don't care about the existing table : simple, you can remove the @ORM\JoinTable(name="categories") annotation, and doctrine will create an other table etc.

  • You want to keep your existing table, which sounds pretty logical : you have to be more explicit in your annotation by adding @ORM\JoinColumn annotation.

Here is an example:

class 

<?php
...
/**
 * @ORM\Entity
 * @ORM\Table(name="tasks") 
 */
class Task
{
    ...
    /**
     * @ORM\ManyToMany(targetEntity="Category", inversedBy="tasks")
     * @ORM\JoinTable(name="categories",
     *       joinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id")},
     *       inverseJoinColumns={@ORM\JoinColumn(name="task_id", referencedColumnName="id")})                         
     */
    protected $category;
    ...
}

and Category object

<?php
...    
/**
 * @ORM\Entity
 * @ORM\Table(name="categories") 
 */
class Category
{
    ...
    /**
     * @ORM\ManyToMany(targetEntity="Task", mappedBy="category")
     * @ORM\JoinTable(name="categories",
     *       joinColumns={@ORM\JoinColumn(name="task_id", referencedColumnName="id")},
     *       inverseJoinColumns={@ORM\JoinColumn(name="category_id", referencedColumnName="id")})
     */
    private $tasks;            
...

Doing so, you will be able to keep your table without any doctrine error.


My fix for this, as far as I can tell, was a case-sensitivity issue with table names. Doctrine let me create a Users and a users table but afterwards would die on migrations:diff or migrations:migrate .

I used the -vvv option to get more detail on this error message; it seems that the error happens when Doctrine is loading up its own internal representation of the current database's schema. So if your current database has table names that Doctrine doesn't understand (like two tables that are identical, case-insensitive) then it will blow up in this fashion.

Seems like most of the answers above assume that the error is in your code, but in my case it was in the database.