Majdi Allagui
Majdi Allagui

Reputation: 11

An exception occurred while executing 'ALTER TABLE

i have a problem with many to many association i have 2 entity one named user

/**
 * @ManyToMany(targetEntity="DataSiteBundle\Entity\Site", mappedBy="user")
 * @ORM\JoinColumn(
 *     name="id_site",
 *     referencedColumnName="id_site",
 *     nullable=false,
 *     onDelete="CASCADE"
 * )
 */
private $site;

and the other is named site

/**
 * @Groups({"site-read","site-write"})
 * @ORM\ManyToMany(targetEntity="UserBundle\Entity\User" , inversedBy="site")
 * @ORM\JoinColumn(
 *     name="id_user",
 *     referencedColumnName="id_user",
 *     nullable=false,
 *     onDelete="CASCADE"
 * )
 *
 */
private $user;

when im trying to do doctrine:schema:update --force

i got this In AbstractMySQLDriver.php line 121:

An exception occurred while executing 'ALTER TABLE site_user ADD CONSTRAINT FK_B6096BB0F6BD1646 FOREIGN KEY (site_id) REFERENCES sp_site (id) ON DELETE CASCADE':

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

Upvotes: 0

Views: 4634

Answers (1)

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35963

Your problem is that there is a table with an existing constraint. I think there are two solutions

If you are in dev, you can rebuild your database

php bin/console doctrine:database:drop --force
php bin/console doctrine:database:create
php bin/console doctrine:schema:update --force

If you are in production I think that you need to make a script to get your data, save somewhere, delete table and remake it, or create a script in sql to skip this problem

Upvotes: 1

Related Questions