Samo
Samo

Reputation: 8240

Zend/Doctrine can't perform ORM operations, table already exists

I inherited a project from another team and can't seem to do anything with the database. I am a total n00b with zend & doctrine, but the ORM tool seems straightforward enough; however, when trying to use it, I get the same error from orm:schema-tool:drop, orm:schema-tool:create, orm:schema-tool:update, etc.

  [Doctrine\DBAL\Schema\SchemaException]                            
  The table with name 'mydb.alerts_residents' already exists.

My database has been created but has no tables. Other posts I've read lead me to conclude that this message is based on my Entity object definitions and annotations.

As you might suspect, alerts_residents is a join table that connects the Alert entity with the Resident entity in a many-to-many relationship. These are the only entities that reference this table, and they appear to do so correctly.

class Resident
{

    /**
     * @var ArrayCollection $zone
     *
     * @ORM\ManyToMany(targetEntity="Alert")
     * @ORM\JoinTable(
     *      name="alerts_residents",
     *      joinColumns={@ORM\JoinColumn(name="resident_id", referencedColumnName="id", onDelete="CASCADE")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="alert_id", referencedColumnName="id", onDelete="CASCADE")}
     * )
     */
    protected $alerts;


class Alert
{
    /**
     * @var ArrayCollection $zone
     *
     * @ORM\ManyToMany(targetEntity="Resident")
     * @ORM\JoinTable(
     *      name="alerts_residents",
     *      joinColumns={@ORM\JoinColumn(name="alert_id", referencedColumnName="id", onDelete="CASCADE")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="resident_id", referencedColumnName="id", onDelete="CASCADE")}
     * )
     */
    protected $residents;

There is no entity with @ORM\Table(name="alerts_residents"). Why am I getting this error?

Upvotes: 1

Views: 303

Answers (1)

dualmon
dualmon

Reputation: 1236

You've got the many-to-many defined redundantly. M2M associations are a bit odd, because the join table has no entity of its own. As such, one side gets (more or less) arbitrarily chosen as the owning side, and that is the entity which gets the join table details. Your example has the @ORM\JoinTable annotation on both.

See http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#owning-and-inverse-side-on-a-manytomany-association

Upvotes: 1

Related Questions