douzhizao0270 2016-03-11 21:49
浏览 34

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

I am using Symfony 3 and Doctrine with a Postgresql database. I generate a basic class with a few properties:

namespace Dmfa\AdminBundle\Entity;

use Doctrine\ORM\Mapping as 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;
    }
}

then i generate a diff and do a migration. It work perfect. I then at a Company property to the entity:

    /**
     * @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;
    }

i do a diff and notice that it does not do a alter table instead it seems to be recreating the table. I get the following errors:

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

Is his a bug or is this my own wrong doing. Please advise on how to correct this.

***** Edit *****

After further analysis the migrations don't even seem to be right. Up and down have the same exact code for the send migration which doesnt include any alter or drops.

Second Migration:

    /**
     * @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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
    • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
    • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
    • ¥15 运动想象脑电信号数据集.vhdr
    • ¥15 三因素重复测量数据R语句编写,不存在交互作用
    • ¥15 微信会员卡等级和折扣规则
    • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
    • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
    • ¥15 gdf格式的脑电数据如何处理matlab
    • ¥20 重新写的代码替换了之后运行hbuliderx就这样了