Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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..

like image 570
user231791 Avatar asked Aug 20 '13 17:08

user231791


People also ask

How do you check if a table already exists in SQL?

Using the OBJECT_ID and the IF ELSE statement to check whether a table exists or not. Alternative 2 : Using the INFORMATION_SCHEMA. TABLES and SQL EXISTS Operator to check whether a table exists or not.

How can I tell if a table exists in a database?

SELECT TABLE_NAME FROM INFORMATION_SCHEMA. TABLES WHERE TABLE_SCHEMA = 'yourDatabaseName' AND TABLE_NAME = 'yourTableName'; Let us implement the above syntax in order to check if a table already exists in the database.


2 Answers

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.

like image 85
BenMorel Avatar answered Nov 03 '22 12:11

BenMorel


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

like image 1
Zoltán Süle Avatar answered Nov 03 '22 11:11

Zoltán Süle