blakyris
blakyris

Reputation: 181

Symfony - SQLSTATE-23000 : Integrity constraint violation: 1022 Can't write duplicate key

I would like to use relations in my Doctrine Product, ProductType and Manufacturer entities, but when i do php bin/console doctrine:migrations:migrate I get this error :

-> ALTER TABLE product ADD CONSTRAINT FK_D34A04ADA23B42D FOREIGN KEY (manufacturer_id) REFERENCES manufacturer (id)
Migration 20180223095324 failed during Execution. Error An exception occurred while executing 'ALTER TABLE product ADD CONSTRAINT FK_D34A04ADA23B42D FOREIGN KEY (manufacturer_id) REFERENCES manufacturer (id)':

SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table '#sql-36a_f'

Here is my relations in the Product Entity :

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\ProductType", inversedBy="products")
 * @ORM\JoinColumn(name="type_id", referencedColumnName="id", nullable=true)
 */
private $type;

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\Manufacturer", inversedBy="products")
 * @ORM\JoinColumn(name="manufacturer_id", referencedColumnName="id", nullable=true)
 */
private $manufacturer;

My ProductType entity :

/**
* @ORM\OneToMany(targetEntity="App\Entity\Product", mappedBy="type")
*/
private $products;

/**
 * @return Collection|Product[]
 */
public function getProducts()
{
    return $this->products;
}

public function __construct()
{
    $this->products = new ArrayCollection();
}

My Manufacturer entity :

/**
* @ORM\OneToMany(targetEntity="App\Entity\Product", mappedBy="manufacturer")
*/
private $products;

/**
 * @return Collection|Product[]
 */
public function getProducts()
{
    return $this->products;
}

public function __construct()
{
    $this->products = new ArrayCollection();
}

Can I use multiple relations in one entity ? Or can I rename foreign keys ?

Upvotes: 3

Views: 965

Answers (1)

blakyris
blakyris

Reputation: 181

You have to update the scheme via : bin/console doctrine:schema:update

Upvotes: 1

Related Questions