user231791
user231791

Reputation: 301

Table already exist

I am using doctrine 2 in zend framework 2. Below is my entity file. The problem is, when I tried to validate schema using,

./vendor/bin/doctrine-module orm:validate-schema

command.

I am getting error,

[Doctrine\DBAL\Schema\SchemaException]                               
The table with name 'database.opportunitycriteria' already exists.

What should I do?

namespace Administration\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * OpportunityCriteria
 *
 * @ORM\Table(name="OpportunityCriteria")
 * @ORM\Entity
 */
class Criteria
{
/**
 * @var integer
 * @ORM\Id
 * @ORM\Column(name="criteria_id", type="integer", nullable=false)
 */
private $criteria_id;

/**
 * @var string
 *
 * @ORM\Column(name="description", type="string", nullable=false)
 */
private $description;
}

and appropriate getter and setter methods..

Upvotes: 25

Views: 11281

Answers (2)

Zoltán Süle
Zoltán Süle

Reputation: 1694

it can cause this error message if you want to use a table name which is already used by one of the installed bundles.

Upvotes: 1

BenMorel
BenMorel

Reputation: 36484

I finally figured it out. OP's use case may be different, but in my case, this was because of a misconfigured bidirectional many-to-many relationship.

I had the following entities:

class Cuisine {
    /**
     * @ManyToMany(targetEntity="Dish")
     * @ORM\JoinTable(name="CuisineDish", ...)
     */
    protected $dishes;
}

class Dish {
    /**
     * @ORM\ManyToMany(targetEntity="Cuisine")
     * @ORM\JoinTable(name="CuisineDish", ...)
     */
    protected $cuisines;
}

What was missing was the inversedBy and mappedBy properties of the @ManyToMany annotations. These are only required when the association is bi-directional.

So now the correctly mapped entities look like:

class Cuisine {
    /**
     * @ManyToMany(targetEntity="Dish", inversedBy="cuisines")
     * @ORM\JoinTable(name="CuisineDish", )
     */
    protected $dishes;
}

class Dish {
    /**
     * @ORM\ManyToMany(targetEntity="Cuisine", mappedBy="dishes")
     * @ORM\JoinTable(name="CuisineDish", ...)
     */
    protected $cuisines;
}

And orm:validate-schema does not exit with an exception any more.

The exception message is just misleading, as the database is not altered by this operation. Furthermore, this issue is only spotted when validating the sync with the database, not when validating the mapping only (--skip-sync), where it should.

I just reported this bug.

Upvotes: 17

Related Questions