无法使用 Doctrine 和 Symfony2 在 MySQL 中创建表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7565492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:11:08  来源:igfitidea点击:

Unable to create table in MySQL using Doctrine and Symfony2

mysqlsymfonydoctrine-orm

提问by Anu

I am working with Symfony2 and Doctrine ORM using MySql .

我正在使用 MySql 使用 Symfony2 和 Doctrine ORM。

After creating an Entity, I am not able to create the table. It throws an exception.

创建实体后,我无法创建表。它抛出异常。

anu@anu-bridge:/var/www/Symfony$ php app/console doctrine:schema:update --force --dump-sql

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

doctrine:schema:update [--complete] [--dump-sql] [--force] [--em[="..."]]

I tried to drop it , but still it throws the error.

我试图删除它,但它仍然抛出错误。

anu@anu-bridge:/var/www/Symfony$ php app/console doctrine:schema:drop --force
Dropping database schema...

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

doctrine:schema:drop [--dump-sql] [--force] [--full-database] [--em[="..."]]

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

There is no tables in the database. Cleared all the cache for symfony and doctrine, but still the error is throwing.

数据库中没有表。清除了 symfony 和学说的所有缓存,但仍然抛出错误。

Symfony2 version is 2.0.1 .

Symfony2 版本是 2.0.1 。

回答by Federico Cristina

I've had a similar problem. Most likely you have two entities named Category inside different Bundles. For instance:

我遇到了类似的问题。很可能您在不同的 Bundle 中有两个名为 Category 的实体。例如:

src/Acme/SomeBundle/Entity/Product.php
src/Acme/OtherBundle/Entity/Product.php

Comment one of these files and retry the console command.

注释这些文件之一并重试控制台命令。

回答by Onshop

I was getting this problem from a conflict with join table defined in an association class annotation and a join table defined in a ManyToMany annotation.

我从关联类注释中定义的连接表和 ManyToMany 注释中定义的连接表的冲突中得到这个问题。

The mapping definitions in two entities with a direct ManytoMany relationship appeared to result in the automatic creation of the join table using the 'joinTable' annotation. However the join table was already defined by an annotation in its underlying entity class and I wanted it to use this association entity class's own field definitions so as to extend the join table with additional custom fields.

具有直接多对多关系的两个实体中的映射定义似乎导致使用“joinTable”注释自动创建连接表。但是,连接表已经由其底层实体类中的注释定义,我希望它使用此关联实体类自己的字段定义,以便使用其他自定义字段扩展连接表。

The explanation and solution was thanks to this post in the forum 'Doctrine Annotation Question'. This post draws attention to the Doctrine documentation regarding ManyToMany Uni-directional relationships. Look at the note regarding the approach of using an 'association entity class' thus replacing the many-to-many annotation mapping directly between two main entity classes with a one-to-many annotation in the main entity classes and two 'many-to-one' annotations in the Associative Entity class. There is an example provided in this forum post Association models with extra fields:

解释和解决方案要感谢论坛“教义注释问题”中的这篇帖子。这篇文章提请注意关于多对多单向关系的 Doctrine 文档。查看有关使用“关联实体类”的方法的注释,从而将两个主要实体类之间的多对多注释映射直接替换为主要实体类中的一对多注释和两个“多对多”注释。 -one' 关联实体类中的注释。本论坛帖子中提供了一个带有额外字段的关联模型示例:

public class Person
{
    /** 
     * @OneToMany(targetEntity="AssignedItems", mappedBy="person")
     */
    private $assignedItems;
}

public class Items
{
    /**
     * @OneToMany(targetEntity="AssignedItems", mappedBy="item")
     */
    private $assignedPeople;
}

public class AssignedItems
{
    /** 
     * @ManyToOne(targetEntity="Person")
     * @JoinColumn(name="person_id", referencedColumnName="id")
     */
    private $person;

    /** 
     * @ManyToOne(targetEntity="Item")
     * @JoinColumn(name="item_id", referencedColumnName="id")
     */
    private $item;
}

回答by sglessard

I got this error when editing my Product.orm.yml file.

编辑我的 Product.orm.yml 文件时出现此错误。

I added a new manyToMany relation with a Category entity, and made a mistake on the joinTable line :

我添加了一个与 Category 实体的新 manyToMany 关系,并在 joinTable 行上犯了一个错误:

manyToMany:
  categories:
    targetEntity: Acme\ProductBundle\Entity\Category
    inversedBy: products
    joinTable:
      name: Product  # My mistake: joinTable should be something like ProductCategory

    [...]

Indeed it's a silly error, I share anyway.

确实这是一个愚蠢的错误,无论如何我都会分享。

回答by crmpicco

If you can, you can do this as this worked for me:

如果可以,您可以这样做,因为这对我有用:

Drop the entire database:

删除整个数据库:

app/console doctrine:schema:drop --force --full-database

app/console doctrine:schema:drop --force --full-database

Run all DB migrations:

运行所有数据库迁移:

app/console doctrine:migrations:migrate

app/console doctrine:migrations:migrate

回答by PiotrC

I imagine It can happen quite often when copying entities. In my case it was a ORM table name annotation that was misfortunately duplicated.

我想在复制实体时它会经常发生。就我而言,它是一个不幸重复的 ORM 表名注释。

/**
 * @ORM\Entity()
 * @ORM\Table(name="category")
 * */
class Category {

回答by Fabian Picone

I had this problem with an One-To-Many, Unidirectional with Join Tablerelation like (see doctrine doc). I did not find this error case with this relation type through the internet or stackoverflow, so i post here my solution for let help others with the same problem.

我在一对多,单向的连接表关系中遇到了这个问题,例如(参见学说文档)。我没有通过互联网或 stackoverflow 找到这种关系类型的错误案例,所以我在这里发布我的解决方案,让帮助其他人解决同样的问题。

What caused this problem:

导致此问题的原因:

  1. After reverse engineering the legacy db tables with ORM (see doc "How to Generate Entities from an Existing Database"), all tables also only join tablesrecieved an entity PHP class.
  2. With annotations on a category entity i described the joining. Results this code:
  1. 使用 ORM 对旧数据库表进行逆向工程后(请参阅文档“如何从现有数据库生成实体”),所有表也仅连接接收到实体 PHP 类的
  2. 通过对类别实体的注释,我描述了加入。结果此代码:
/**
 * @ORM\ManyToMany(targetEntity="Category")
 * @ORM\JoinTable(name="category_child",
 *     joinColumns={@JoinColumn(name="category_id", referencedColumnName="id")},
 *     inverseJoinColumns={@JoinColumn(name="category_child_id", referencedColumnName="id")}
 * )
 */
public $children;
  1. The @ORM\JoinTable(name="category_child"caused that doctrine wants to create this table again. One time because of the already existing Category_Child entity, and then the @ORM\JoinTableexpression that points to the same table.
  1. @ORM\JoinTable(name="category_child"引起的学说希望再次创建该表。有一次因为已经存在 Category_Child 实体,然后@ORM\JoinTable是指向同一个表的表达式。

Solution

解决方案

Solution was to delete the Category_Child entity that was created from reverse engineering. If you used Category_Child entity in some $em queries, you have to select those datas othwerwise. E.g. Through the parent that holds those child datas in an ArrayCollection, or over DBAL.

解决方案是删除从逆向工程中创建的 Category_Child 实体。如果您在某些 $em 查询中使用了 Category_Child 实体,则必须另外选择这些数据。例如,通过在 ArrayCollection 中或通过 DBAL 保存这些子数据的父级。