我将 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;
}
我做了一个差异,并注意到它没有做一个改变表,而是它似乎正在重新创建表。我收到以下错误:
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
是他的错误还是我自己做错了。请告知如何纠正此问题。
***** 编辑 *****
经过进一步分析,迁移甚至似乎都不正确。Up 和 Down 对于发送迁移具有相同的确切代码,其中不包括任何更改或丢弃。
第二次迁移:
/**
* @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');
}