Symfony 1.4 迁移抛出 SQLSTATE [HY000]: 一般错误: 1005 (err 150) - 学说:迁移失败!
这是我自己无法解决的情况。
我有一个现有的 symfony 1.4 项目,其中包含数据库、模型等。我想进行迁移,所以我已经完成了:
./symfony cc
./symfony doctrine:generate-migrations-db
./symfony doctrine:generate-migrations-model
创建了用于迁移的所有类,所以我尝试通过以下方式应用迁移:
./symfony doctrine:drop-db
./symfony doctrine:build-db
./symfony doctrine migrate
迁移过程崩溃。它抛出一个错误:
- SQLSTATE[1005]: General error: 1005 Can't create table 'database.#sql-6df_301' (errno: 150). Failing Query: "ALTER TABLE product ADD CONSTRAINT "product_product_group_id_product_group_id FOREIGN KEY (product_group_id) REFERENCES product_group(id) ON DELETE CASCADE
这很奇怪。我有更多带有关系的表,并且它们已创建,但该表失败了。我已经检查了我能想到的一切。索引类型、表类型、月相——一切似乎都很好。我尝试设置 FOREIGN_KEY_CHEKS=0 来追逐一些鬼魂,但是 NADA!
错误仍然发生。
有谁知道发生了什么或一种解决方案?
有什么建议吗?
Here is a situation i could not resolve by myself.
I have an existing symfony 1.4 project with database, model and etc. I want to make a migration so I've done:
./symfony cc
./symfony doctrine:generate-migrations-db
./symfony doctrine:generate-migrations-model
All classes for the migration were created, so I've tried to apply the migration by:
./symfony doctrine:drop-db
./symfony doctrine:build-db
./symfony doctrine migrate
And the migration proccess crashes. It thros an error:
- SQLSTATE[1005]: General error: 1005 Can't create table 'database.#sql-6df_301' (errno: 150). Failing Query: "ALTER TABLE product ADD CONSTRAINT "product_product_group_id_product_group_id FOREIGN KEY (product_group_id) REFERENCES product_group(id) ON DELETE CASCADE
This is strange. I have more tables with relations and they are created, but that one fails. I've check everything i can think of. The indexes types, the table types, the phase of the moon - everything seems to be OK. I've try to SET FOREIGN_KEY_CHEKS=0 to chase some ghosts, but NADA!
The error still occures.
Does anybody knows what is happening or a kind of solution?
Any suggestion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不应该使用 :generate-migrations-models 。尝试使用以下序列
该行
使用模型类来生成迁移。这并不总是一个好主意,因为您可能已经忘记了模型会把事情搞砸。为了清理那些被遗忘的模型,请在启动上述命令之前使用以下命令。
You should not use :generate-migrations-models . Try it with the sequence
The line
uses the model classes to generate the migrations. It's not always a good idea, because you could have forgotten models messing things up. In order to clean those forgotten models use the following before you start those commands above.
我已经破解了。
出于某种搞笑的原因,自动生成的迁移类的命名如下:
13116785578_addfks.php
文件出现在1311678582_addproductgroup.php 之前
。文件
13116785578_addfks.php
包含一个创建所有关系(外键和约束)的类,但它尝试在不存在的表上添加约束(在我的例子中product_group
,将最后创建)。因此,SQL 错误意味着你正在尝试与不存在的表创建关系,你这个白痴!
:)我的解决方案是将
13116785578_addfks.php
重命名为13116785590_addfks .php
,因为它强制学说合并机将其作为最后一步执行。执行时,所有表都已创建,MySQL 服务器很高兴!那么,这种错误排序的原因是什么呢?
可能是由于混合了任务
doctrine:generate-migrations-db
和doctrine:生成迁移模型
,但是 Symfony 的迁移教程对此不是很清楚。
为什么我同时使用它们?
当我完成
./symfonydoctrine:generate-migration-db
时,只创建了一半的迁移类。很奇怪——只有通用表,没有相关表!所以我调用了doctrine:generate-migrations-models
。结论
还有一个问题:为什么
doctrine:generate-migrations-db
不为模型的相关表生成迁移类?I've cracked it up.
In a some hilarious reason the auto-generated migration classes are named like this:
The
13116785578_addfks.php
file appears before1311678582_addproductgroup.php
.The file
13116785578_addfks.php
containing a class which creates all relations (foreign keys and constraints), but it tries to add constraint on a non existing table (in my caseproduct_group
, which will be created last). So, the SQL error meansYOU ARE TRYING TO CREATE A RELATION WITH UNEXISTING TABLE, YOU MORON!
:)My soltion is to rename the
13116785578_addfks.php
to13116785590_addfks.php
as it forces the doctrine merge machine to execute it as a last step. When executed all tables are already made and MySQL server is happy!So, what about the reason for this miss-ordering?
Probably it caused by mixi'n up the to tasks
doctrine:generate-migrations-db
anddoctrine:generate-migrations-models
,but the Symfony's Migration tutorial isn't very clear with this.
Why I use them both?
When I done
./symfony doctrine:generate-migration-db
only the half of classes for the migration was created. It is strange - there are only general tables and no related ones! So I calleddoctrine:generate-migrations-models
.Conclusion
Another question raises: Why
doctrine:generate-migrations-db
does not generate migration classes for related tables of the model?