无法使用 Doctrine 和 Symfony2 在 MySQL 中创建表
我正在使用 MySql 来使用 Symfony2 和 Doctrine ORM。
创建实体后,我无法创建表。它抛出异常。
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[="..."]]
我试图删除它,但它仍然抛出错误。
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.
数据库中没有表。清除了symfony和doctrine的所有缓存,但仍然抛出错误。
Symfony2 版本是 2.0.1 。
I am working with Symfony2 and Doctrine ORM using MySql .
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.
Symfony2 version is 2.0.1 .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我也遇到过类似的问题。您很可能在不同的 Bundle 中有两个名为 Category 的实体。例如:
注释这些文件之一并重试控制台命令。
I've had a similar problem. Most likely you have two entities named Category inside different Bundles. For instance:
Comment one of these files and retry the console command.
我之所以遇到这个问题,是因为关联类注释中定义的联接表和多对多注释中定义的联接表发生冲突。
具有直接多对多关系的两个实体中的映射定义似乎导致使用“joinTable”注释自动创建连接表。但是,连接表已由其基础实体类中的注释定义,我希望它使用此关联实体类自己的字段定义,以便使用其他自定义字段扩展连接表。
解释和解决方案归功于论坛'Doctrine Annotation Question中的这篇文章'。这篇文章提请注意有关 多对多单向关系。查看有关使用“关联实体类”的方法的注释,从而用主实体类中的一对多注释和两个“多对”直接替换两个主实体类之间的多对多注释映射。关联实体类中的“-one”注释。此论坛帖子具有额外字段的关联模型中提供了一个示例:
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.
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.
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:
我在编辑 Product.orm.yml 文件时遇到此错误。
我添加了一个与 Category 实体的新的多对多关系,并在 joinTable 行上犯了一个错误:
确实这是一个愚蠢的错误,无论如何我都会分享。
I got this error when editing my Product.orm.yml file.
I added a new manyToMany relation with a Category entity, and made a mistake on the joinTable line :
Indeed it's a silly error, I share anyway.
如果可以的话,您可以这样做,因为这对我有用:
删除整个数据库:
app/consoledoctrine:schema:drop --force --full-database
运行所有数据库迁移:
应用程序/控制台学说:迁移:迁移
If you can, you can do this as this worked for me:
Drop the entire database:
app/console doctrine:schema:drop --force --full-database
Run all DB migrations:
app/console doctrine:migrations:migrate
我想复制实体时经常会发生这种情况。就我而言,这是一个不幸重复的 ORM 表名称注释。
I imagine It can happen quite often when copying entities. In my case it was a ORM table name annotation that was misfortunately duplicated.
我遇到了一个一对多,单向连接表关系的问题,例如(参见学说文档)。我没有通过互联网或 stackoverflow 找到这种关系类型的错误案例,所以我在这里发布我的解决方案,以帮助其他遇到同样问题的人。
导致此问题的原因:
@ORM\JoinTable(name="category_child"
导致该学说想要再次创建此表。一次是因为已经存在 Category_Child 实体,然后@ORM指向同一个表的 \JoinTable
表达式解决方案 解决
方案是删除通过逆向工程创建的 Category_Child 实体 如果您在某些 $em 查询中使用了 Category_Child 实体,则必须选择这些数据。否则,例如通过将这些子数据保存在 ArrayCollection 中的父级,或通过 DBAL。
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 stackoverflow, so i post here my solution for let help others with the same problem.
What caused this problem:
@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.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.