0

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');
    }
NikiC
  • 100,734
  • 37
  • 191
  • 225
Dblock247
  • 6,167
  • 10
  • 44
  • 66
  • 1
    Reduce your example. Give us the minimum amount of code that reproduces the same errors. – Mihai Stancu Mar 11 '16 at 22:34
  • @MihaiStancu My apologies please see above. I trimmed a bunch out please let me know if thats not enough. – Dblock247 Mar 11 '16 at 22:41
  • How looks result of `php app/console doctrine:schema:update --dump-sql` – Paweł Mikołajczuk Mar 12 '16 at 19:15
  • @PawełMikołajczuk wouldn't it be doctrine:migrations:migrate? – Dblock247 Mar 12 '16 at 20:18
  • `php app/console doctrine:schema:update --dump-sql` will tell us if Symfony see your database schema correctly. – Paweł Mikołajczuk Mar 12 '16 at 20:33
  • @PawełMikołajczuk the result is: ALTER TABLE events ADD Company VARCHAR(255) NOT NULL;. I think myabe i was misunderstanding. I thought that after i do php ./bin/console doctrine:migrations:diff. I should do php ./bin/console doctrine:migrations:migrate. but your saying i should just do schema update? (symfony 3 changed to php ./bin/console) – Dblock247 Mar 12 '16 at 20:37
  • no no... schema:update result was just more informations for me to understand your current situation. But it tells me that doctrine see your database correctly. – Paweł Mikołajczuk Mar 12 '16 at 20:39
  • @PawełMikołajczuk the funny things is that it works with schema update and not migrations migrate. altered the table correctly. I did have to set nullable=true on company to make it work. but thats expected or a default value – Dblock247 Mar 12 '16 at 20:42
  • @PawełMikołajczuk is that suppose to happen or is something wrong? – Dblock247 Mar 12 '16 at 20:48
  • It doesn't solve your issue with migrations. And i don't know why you get CREATE instead ALTER. – Paweł Mikołajczuk Mar 12 '16 at 20:50

0 Answers0