Symfony 3 Doctrine 2 Postgresql 9.5迁移试图创建表而不是Alter表php7

时间:2016-03-11 21:49:53

标签: php doctrine-orm symfony postgresql-9.5 doctrine-migrations

我正在使用Symfony 3和Doctrine和Postgresql数据库。我生成了一个具有一些属性的基本类:

命名空间Dmfa \ AdminBundle \ Entity;

将Doctrine \ ORM \ Mapping用作ORM;

/**
 * Event
 *
 * @ORM\Table(name="events", schema="public")
 * @ORM\Entity(repositoryClass="Dmfa\AdminBundle\Repository\EventRepository")
 */
class Event
{
    /**
     * @var int
     *
     * @ORM\Column(name="Id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

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

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

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

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

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

    /**
     * Set name
     *
     * @param string $name
     *
     * @return Event
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Set location
     *
     * @param string $location
     *
     * @return Event
     */
    public function setLocation($location)
    {
        $this->location = $location;

        return $this;
    }

    /**
     * Get location
     *
     * @return string
     */
    public function getLocation()
    {
        return $this->location;
    }

    /**
     * Set time
     *
     * @param \DateTime $time
     *
     * @return Event
     */
    public function setTime($time)
    {
        $this->time = $time;

        return $this;
    }

    /**
     * Get time
     *
     * @return \DateTime
     */
    public function getTime()
    {
        return $this->time;
    }

    /**
     * Set details
     *
     * @param string $details
     *
     * @return Event
     */
    public function setDetails($details)
    {
        $this->details = $details;

        return $this;
    }

    /**
     * Get details
     *
     * @return string
     */
    public function getDetails()
    {
        return $this->details;
    }
}

然后我生成差异并进行迁移。它完美无缺。然后我在公司财产中向实体发送:

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

    /**
     * @return string
     */
    public function getCompany()
    {
        return $this->company;
    }

    /**
     * @param string $company
     * @return Event
     */
    public function setCompany($company)
    {
        $this->company = $company;
        return $this;
    }

我做了一个差异并注意到它没有做一个alter table而是它似乎正在重新创建表。我收到以下错误:

Migration 20160311163807 failed during Execution. Error An exception occurred while executing 'CREATE TABLE public.events (Id SERIAL NOT NULL, Name VARCHAR(255) NOT NULL, Location VARCHAR(255) NOT NULL, Time TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, Details VARCHAR(255) NOT NULL, Company VARCHAR(255) NOT NULL, PRIMARY KEY(Id))':

SQLSTATE[42P07]: Duplicate table: 7 ERROR:  relation "events" already exists


[Doctrine\DBAL\Exception\TableExistsException]                                                                                                                                                                                                                    
  An exception occurred while executing 'CREATE TABLE public.events (Id SERIAL NOT NULL, Name VARCHAR(255) NOT NULL, Location VARCHAR(255) NOT NULL, Time TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, Details VARCHAR(255) NOT NULL, Company VARCHAR(255) NOT NULL, P  
  RIMARY KEY(Id))':                                                                                                                                                                                                                                                 
  SQLSTATE[42P07]: Duplicate table: 7 ERROR:  relation "events" already exists 

[Doctrine\DBAL\Driver\PDOException]                                           
  SQLSTATE[42P07]: Duplicate table: 7 ERROR:  relation "events" already exists

[PDOException]                                                                
  SQLSTATE[42P07]: Duplicate table: 7 ERROR:  relation "events" already exists

他是一个错误还是我自己做错了。请告知如何纠正此问题。

*****编辑*****

经过进一步分析后,迁移似乎不太合适。向上和向下具有与发送迁移完全相同的代码,不包括任何更改或删除。

第二次迁移:

    /**
     * @param Schema $schema
     */
    public function up(Schema $schema)
    {
        // this up() migration is auto-generated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');

        $this->addSql('DROP SEQUENCE events_id_seq CASCADE');
        $this->addSql('CREATE TABLE public.events (Id SERIAL NOT NULL, Name VARCHAR(255) NOT NULL, Location VARCHAR(255) NOT NULL, Time TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, Details VARCHAR(255) NOT NULL, Company VARCHAR(255) NOT NULL, PRIMARY KEY(Id))');
        $this->addSql('DROP TABLE events');
    }

    /**
     * @param Schema $schema
     */
    public function down(Schema $schema)
    {
        // this down() migration is auto-generated, please modify it to your needs
        $this->abortIf($this->connection->getDatabasePlatform()->getName() != 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');

        $this->addSql('CREATE SEQUENCE events_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
        $this->addSql('CREATE TABLE events (id SERIAL NOT NULL, name VARCHAR(255) NOT NULL, location VARCHAR(255) NOT NULL, "time" TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, details VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
        $this->addSql('DROP TABLE public.events');
    }

0 个答案:

没有答案