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');
}