Symfony2 and Doctrine: A table named "blog.post" already exists.

I am working with Symfony2 and Doctrine ORM using MySql. When I try to use uo:

php app/console doctrine:migration:diff 

I have this error:

 [Doctrine\DBAL\Schema\SchemaException] The table with name 'blog.post' already exists. 

My code in Post.php (I use annotation):

  namespace Blog\ModelBundle\Entity; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; /** * Post * * @ORM\Table() * @ORM\Entity */ class Post extends Timestampable { /** * @var integer * * @ORM\Column(name="id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var string * * @ORM\Column(name="title", type="string", length=150) * @Assert\NotBlank */ private $title; /** * @var string * * @ORM\Column(name="body", type="text") * @Assert\NotBlank */ private $body; /** * @var Author * @ORM\ManyToOne (targetEntity="Author", inversedBy="posts") * @ORM\JoinColumn (name="author_id", referencedColumnName="id", nullable=false) * @Assert\NotBlank */ private $author; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set title * * @param string $title * @return Post */ public function setTitle($title) { $this->title = $title; return $this; } /** * Get title * * @return string */ public function getTitle() { return $this->title; } /** * Set body * * @param string $body * @return Post */ public function setBody($body) { $this->body = $body; return $this; } /** * Get body * * @return string */ public function getBody() { return $this->body; } /** * Set author * * @param \Blog\ModelBundle\Entity\Author $author * @return Post */ public function setAuthor(Author $author) { $this->author = $author; return $this; } /** * Get author * * @return \Blog\ModelBundle\Entity\Author */ public function getAuthor() { return $this->author; } } 

I am trying to define * @ORM \ Table (name = "Post").

Can you help me with this type of error. Sorry for my bad english.

+5
source share
1 answer

Your mapping does not look right. The ManyToOne declaration does not require the inversedBy attribute if you are not using a join table. You also do not need to specify @var, since it already knows it with an entity. Try the following:

 /** * @ORM\ManyToOne(targetEntity="Author") * @ORM\JoinColumn(name="author_id", referencedColumnName="id") **/ private $author; 

Another thing that needs to be done is to verify that you are not trying to declare the same object in another package, this will also result in a "table already exists" error.

In addition, to avoid using references to objects of the full path in getter and setter, just include the entity in the use of statemnets at the top of the class, then you only need to write the name of the object:

 /** * set Author * * @param Author $author * * @return Post /** 
+1
source

Source: https://habr.com/ru/post/1209917/


All Articles