Symfony2在每次更新时重新创建表

时间:2015-08-21 11:23:10

标签: php mysql sql-server symfony doctrine-orm

从今天起,我发生了一个最奇怪的问题。当我尝试更新数据库模式时,我的两个表总是试图重新创建...

例如,我有两个表,campaign_list和campaign_article ...让我说我创建这些表..然后我说我向名为User的实体添加一个新字段。我尝试更新数据库:

php app/console doctrine:schema:update --force

我收到一个错误,即campaign_article和campaign_list已经存在。当然,他们存在,他为什么要创建新表?

好吧,那就让我说删除旧表..然后进行更新,一切都更新..

然后我将某些内容改为其他enitites,进行更新,我再次得到同样的错误......

发生了什么事?

$ php app/console doctrine:schema:update --force
Updating database schema...



  [Doctrine\DBAL\Exception\TableExistsException]
  An exception occurred while executing 'CREATE TABLE campaign_article (id INT AUTO_INCREMENT N
  OT NULL, id_campaign INT DEFAULT NULL, article INT DEFAULT NULL, id_rule INT DEFAULT NULL, fa
  ctory_price VARCHAR(255) DEFAULT NULL, selling_price VARCHAR(255) NOT NULL, stock VARCHAR(255
  ) NOT NULL, reserved VARCHAR(255) DEFAULT NULL, real_price VARCHAR(255) DEFAULT NULL, INDEX I
  DX_AA80924A4FCEFE48 (id_campaign), INDEX IDX_AA80924A23A0E66 (article), PRIMARY KEY(id)) DEFA
  ULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB':
  SQLSTATE[42S01]: Base table or view already exists: 1050 La table 'campaign_article' existe d
  éjà                                                                                           

  [Doctrine\DBAL\Driver\PDOException]
  SQLSTATE[42S01]: Base table or view already exists: 1050 La table 'campaign_article' existe d
  éjà                                                                                           


  [PDOException]
  SQLSTATE[42S01]: Base table or view already exists: 1050 La table 'campaign_article' existe d
  éjà   

昨天我将两个分支合并在一起,这会导致问题吗?

$ php app/console doctrine:schema:valid
[Mapping]  OK - The mapping files are correct.
[Database] FAIL - The database schema is not in sync with the current mapping file.

$ php app/console doctrine:schema:update --dump-sql
CREATE TABLE campaign_article (id INT AUTO_INCREMENT NOT NULL, id_campaign INT DEFAULT NULL, article INT DEFAULT NULL, id_rule INT DEFAULT NULL, factory_price VARCHAR(255) DEFAULT NULL, selling_price VARCHAR(255) NOT NULL, stock VARCHAR(255) NOT NULL, reserved VARCHAR(255) DEFAULT NULL, real_price VARCHAR(255) DEFAULT NULL, INDEX IDX_AA80924A4FCEFE48 (id_campaign), INDEX IDX_AA80924A23A0E66 (article), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
CREATE TABLE campaign_list (id INT AUTO_INCREMENT NOT NULL, id_user INT DEFAULT NULL, name VARCHAR(255) NOT NULL, start_date DATETIME NOT NULL, end_date DATETIME DEFAULT NULL, state TINYINT(1) NOT NULL, id_purchase_order INT DEFAULT NULL, id_sale_order INT DEFAULT NULL, customer VARCHAR(255) DEFAULT NULL, location VARCHAR(255) DEFAULT NULL, INDEX IDX_452FC3FA6B3CA4B (id_user), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
ALTER TABLE campaign_article ADD CONSTRAINT FK_AA80924A4FCEFE48 FOREIGN KEY (id_campaign) REFERENCES campaign_list (id);
ALTER TABLE campaign_article ADD CONSTRAINT FK_AA80924A23A0E66 FOREIGN KEY (article) REFERENCES product (id);
ALTER TABLE campaign_list ADD CONSTRAINT FK_452FC3FA6B3CA4B FOREIGN KEY (id_user) REFERENCES fos_user_user (id);

CampaignArticle.php:

/**
 * CampaignArticle
 *
 * @ORM\Table(name="campaign_article")
 * @ORM\Entity
 */
class CampaignArticle
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var CampaignList
     *
     * @ORM\ManyToOne(targetEntity="CampaignList")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="id_campaign", referencedColumnName="id")
     * })
     */
    private $campaignId;

    /**
     * @var integer
     *
     * @ORM\Column(name="id_rule", type="integer", nullable=true)
     */
    private $ruleId;

    /**
     * @var Product
     *
     * @ORM\ManyToOne(targetEntity="Product")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="article", referencedColumnName="id")
     * })
     */
    private $articleId;

    /**
     * @var string
     *
     * @ORM\Column(name="factory_price", type="string", length=255, nullable=true)
     */
    private $factoryPrice;

    /**
     * @var string
     *
     * @ORM\Column(name="selling_price", type="string", length=255)
     */
    private $sellingPrice;

    /**
     * @var string
     *
     * @ORM\Column(name="stock", type="integer", nullable=true)
     */
    private $stock;

    /**
     * @var string
     *
     * @ORM\Column(name="reserved", type="integer", nullable=true)
     */
    private $reserved;

    /**
     * @var string
     *
     * @ORM\Column(name="real_price", type="integer", nullable=true)
     */
    private $realPrice;


    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

CampaignList.php:

/**
 * CampaignList
 *
 * @ORM\Table(name="campaign_list")
 * @ORM\Entity(repositoryClass="Mp\ShopBundle\Repository\CampaignRepository")
 */

class CampaignList
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var \Application\Sonata\UserBundle\Entity\User
     *
     * @ORM\ManyToOne(targetEntity="\Application\Sonata\UserBundle\Entity\User")
     * @ORM\JoinColumns({
     *   @ORM\JoinColumn(name="id_user", referencedColumnName="id")
     * })
     */
    private $userId;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="start_date", type="datetime")
     */
    private $startDate;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="end_date", type="datetime", nullable=true)
     */
    private $endDate;

    /**
     * @var boolean
     *
     * @ORM\Column(name="state", type="boolean")
     */
    private $state;

    /**
     * @var integer
     *
     * @ORM\Column(name="id_purchase_order", type="integer", nullable=true)
     */
    private $purchaseOrderId;

    /**
     * @var integer
     *
     * @ORM\Column(name="id_sale_order", type="integer", nullable=true)
     */
    private $saleOrderId;

    /**
     * @var string
     *
     * @ORM\Column(name="customer", type="string", length=255, nullable=true)
     */
    private $customer;

    /**
     * @var string
     *
     * @ORM\Column(name="location", type="string", length=255, nullable=true)
     */
    private $location;

1 个答案:

答案 0 :(得分:0)

如果您在其他捆绑包中也有相同的邀请,则会发生这种情况。

请检查实体的重复性。