Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 and Doctrine: The table with name 'blog.post' already exists.

i working with Symfony2 and Doctrine ORM using MySql. When i try uo use:

 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) is:

 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 try to define * @ORM\Table(name="Post").

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

like image 524
user3210305 Avatar asked Nov 09 '22 21:11

user3210305


1 Answers

Your mapping looks incorrect. The ManyToOne declaration does not need an inversedBy attribute if you're not using a join table. You also do not need to specify the @var since it already knows it's using the entity. Try this:

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

One other thing to do is to check that you're not trying to declare the same entity in another bundle, this will also cause the "table already exists" error.

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

/**
 * set Author
 *
 * @param Author $author
 *
 * @return Post
/**
like image 89
Michael Emerson Avatar answered Nov 15 '22 10:11

Michael Emerson