【问题标题】:Symfony2 and Doctrine: The table with name 'blog.post' already exists.Symfony2 和 Doctrine:名为“blog.post”的表已经存在。
【发布时间】:2023-03-10 08:13:01
【问题描述】:

我使用 MySql 处理 Symfony2 和 Doctrine ORM。 当我尝试使用 uo 时:

 php app/console doctrine:migration:diff       

我有这个错误:

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

我在 Post.php 中的代码(我使用注释)是:

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

我尝试定义 * @ORM\Table(name="Post")。

你能帮我解决这种类型的错误吗? 对不起我的英语不好。

【问题讨论】:

  • 您有来自另一个实体映射的冲突。它可以是JoinTable 或其他实体。
  • 我只有一个实体 - 作者,我有: /** * @var ArrayCollection * * @ORM\OneToMany(targetEntity="Post", mappedBy="author", cascade={"删除"}) */ 私人 $posts;

标签: symfony doctrine-orm


【解决方案1】:

您的映射看起来不正确。如果您不使用连接表,ManyToOne 声明不需要 inversedBy 属性。您也不需要指定@var,因为它已经知道它正在使用实体。试试这个:

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

另一件要做的事情是检查您是否没有尝试在另一个包中声明相同的实体,这也会导致“表已存在”错误。

另外,为了避免在getter和setter中使用完整路径的实体引用,只需将实体包含在类顶部的use statemnets中,然后你只需要写实体名称:

/**
 * set Author
 *
 * @param Author $author
 *
 * @return Post
/**

【讨论】:

    猜你喜欢
    • 2017-04-04
    • 1970-01-01
    • 2017-07-21
    • 2016-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    • 1970-01-01
    相关资源
    最近更新 更多