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和doctrine的所有缓存,但仍然是错误抛出。
Symfony2 version is 2.0.1 .
Symfony2版本是2.0.1。
6 个解决方案
#1
47
I've had a similar problem. Most likely you have two entities named Category inside different Bundles. For instance:
我遇到过类似的问题。很可能你在不同的Bundles中有两个名为Category的实体。例如:
src/Acme/SomeBundle/Entity/Product.php
src/Acme/OtherBundle/Entity/Product.php
Comment one of these files and retry the console command.
注释其中一个文件并重试控制台命令。
#2
10
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.
具有直接ManytoMany关系的两个实体中的映射定义似乎导致使用'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 Annotation Question”中的这篇文章。这篇文章提请注意有关ManyToMany单向关系的Doctrine文档。查看关于使用“关联实体类”的方法的注释,从而直接在两个主要实体类之间替换多对多注释映射,在主要实体类中使用一对多注释和两个“多 - 到” - 关联实体类中的一个'注释。此论坛中提供了一个示例,其中包含附加字段的关联模型:
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;
}
#3
4
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.
事实上,这是一个愚蠢的错误,无论如何我分享。
#4
1
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:迁移:迁移
#5
1
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 {
#6
0
I had this problem with an One-To-Many, Unidirectional with Join Table relation like (see doctrine doc). I did not find this error case with this relation type through the internet or *, so i post here my solution for let help others with the same problem.
我遇到了一对多,单向与连接表关系的问题(参见doctrine doc)。我没有通过互联网或*找到这种关系类型的错误情况,所以我在这里发布我的解决方案,让其他人解决同样的问题。
What caused this problem:
是什么导致了这个问题
- After reverse engineering the legacy db tables with ORM (see doc "How to Generate Entities from an Existing Database"), all tables also only join tables recieved an entity PHP class.
- 在使用ORM对遗留数据库表进行逆向工程之后(请参阅文档“如何从现有数据库生成实体”),所有表也只连接接收到实体PHP类的表。
- With annotations on a category entity i described the joining. Results this code:
- 通过对类别实体的注释,我描述了加入。结果这段代码:
/**
* @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;
- 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\JoinTable
expression that points to the same table. - @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中保存这些子数据的父级。
#1
47
I've had a similar problem. Most likely you have two entities named Category inside different Bundles. For instance:
我遇到过类似的问题。很可能你在不同的Bundles中有两个名为Category的实体。例如:
src/Acme/SomeBundle/Entity/Product.php
src/Acme/OtherBundle/Entity/Product.php
Comment one of these files and retry the console command.
注释其中一个文件并重试控制台命令。
#2
10
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.
具有直接ManytoMany关系的两个实体中的映射定义似乎导致使用'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 Annotation Question”中的这篇文章。这篇文章提请注意有关ManyToMany单向关系的Doctrine文档。查看关于使用“关联实体类”的方法的注释,从而直接在两个主要实体类之间替换多对多注释映射,在主要实体类中使用一对多注释和两个“多 - 到” - 关联实体类中的一个'注释。此论坛中提供了一个示例,其中包含附加字段的关联模型:
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;
}
#3
4
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.
事实上,这是一个愚蠢的错误,无论如何我分享。
#4
1
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:迁移:迁移
#5
1
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 {
#6
0
I had this problem with an One-To-Many, Unidirectional with Join Table relation like (see doctrine doc). I did not find this error case with this relation type through the internet or *, so i post here my solution for let help others with the same problem.
我遇到了一对多,单向与连接表关系的问题(参见doctrine doc)。我没有通过互联网或*找到这种关系类型的错误情况,所以我在这里发布我的解决方案,让其他人解决同样的问题。
What caused this problem:
是什么导致了这个问题
- After reverse engineering the legacy db tables with ORM (see doc "How to Generate Entities from an Existing Database"), all tables also only join tables recieved an entity PHP class.
- 在使用ORM对遗留数据库表进行逆向工程之后(请参阅文档“如何从现有数据库生成实体”),所有表也只连接接收到实体PHP类的表。
- With annotations on a category entity i described the joining. Results this code:
- 通过对类别实体的注释,我描述了加入。结果这段代码:
/**
* @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;
- 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\JoinTable
expression that points to the same table. - @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中保存这些子数据的父级。