我有以下实体:
A.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="a")
* @ORM\Entity()
*/
class A
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* One Cart has One Customer.
* @ORM\OneToOne(targetEntity="B", inversedBy="a")
* @ORM\JoinColumn(name="b_id", referencedColumnName="id")
*/
private $b;
/**
* @ORM\OneToOne(targetEntity="C", inversedBy="a")
* @ORM\JoinColumn(name="c_id", referencedColumnName="id")
*/
private $c;
/**
* @ORM\ManyToOne(targetEntity="D", inversedBy="a")
* @ORM\JoinColumn(name="d_id", referencedColumnName="id")
*/
private $d;
}
B.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="b")
* @ORM\Entity()
*/
class B
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToOne(targetEntity="A", mappedBy="b")
*/
private $a;
}
C.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="c")
* @ORM\Entity()
*/
class C
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToOne(targetEntity="A", mappedBy="c")
*/
private $a;
}
D.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="d")
* @ORM\Entity()
*/
class D
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="A", mappedBy="d")
*/
private $a;
}
我执行命令:
php bin/console doctrine:database:create
php bin/console doctrine:schema:update --force
php bin/console doctrine:schema:update --dump-sql
最后一个命令打印:
ALTER TABLE a ADD CONSTRAINT FK_E8B7BE43296BFCB6 FOREIGN KEY (b_id) REFERENCES b (id);
但是这个约束是之前添加的,所以我不能使用
php bin/console doctrine:schema:update --force
两次都没有错误。
仅当我使用 MySQL 时才会出现此行为。当我删除 MySQL 并安装 MariaDB 时,问题就消失了。
我应该在我的实体中引入更改,还是 Doctrine 的错误?
最佳答案
这似乎是一个错误。尝试使用 DoctrineMigrations 包,以便您可以在应用迁移类之前对其进行控制。
关于mysql - 更新架构时 Doctrine 添加现有外键(mysql),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44216066/