Symfony 1.4 迁移抛出 SQLSTATE [HY000]: 一般错误: 1005 (err 150) - 学说:迁移失败!

发布于 2024-11-26 01:44:25 字数 843 浏览 1 评论 0原文

这是我自己无法解决的情况。

我有一个现有的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清风挽心 2024-12-03 01:44:25

您不应该使用 :generate-migrations-models 。尝试使用以下序列

./symfony cc
./symfony doctrine:generate-migrations-db
./symfony doctrine:drop-db
./symfony doctrine:build-db
./symfony doctrine migrate

该行

./symfony doctrine:generate-migrations-models

使用模型类来生成迁移。这并不总是一个好主意,因为您可能已经忘记了模型会把事情搞砸。为了清理那些被遗忘的模型,请在启动上述命令之前使用以下命令。

./symfony doctrine:clean-model-files

You should not use :generate-migrations-models . Try it with the sequence

./symfony cc
./symfony doctrine:generate-migrations-db
./symfony doctrine:drop-db
./symfony doctrine:build-db
./symfony doctrine migrate

The line

./symfony doctrine:generate-migrations-models

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.

./symfony doctrine:clean-model-files
神魇的王 2024-12-03 01:44:25

我已经破解了。

出于某种搞笑的原因,自动生成的迁移类的命名如下:

1311678541_add<table-name>.php
1311678542_add<table-name>.php
1311678543_add<table-name>.php
...
13116785578_addfks.php
...
1311678579_add<table-name>.php
1311678580_add<table-name>.php
1311678582_addproductgroup.php

13116785578_addfks.php 文件出现在 1311678582_addproductgroup.php 之前

文件13116785578_addfks.php包含一个创建所有关系(外键和约束)的类,但它尝试在不存在的表上添加约束(在我的例子中product_group,将最后创建)。因此,SQL 错误意味着 你正在尝试与不存在的表创建关系,你这个白痴! :)

我的解决方案是将 13116785578_addfks.php 重命名为 13116785590_addfks .php,因为它强制学说合并机将其作为最后一步执行。执行时,所有表都已创建,MySQL 服务器很高兴!

那么,这种错误排序的原因是什么呢?

可能是由于混合了任务 doctrine:generate-migrations-dbdoctrine:生成迁移模型
但是 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:

1311678541_add<table-name>.php
1311678542_add<table-name>.php
1311678543_add<table-name>.php
...
13116785578_addfks.php
...
1311678579_add<table-name>.php
1311678580_add<table-name>.php
1311678582_addproductgroup.php

The 13116785578_addfks.php file appears before 1311678582_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 case product_group, which will be created last). So, the SQL error means YOU ARE TRYING TO CREATE A RELATION WITH UNEXISTING TABLE, YOU MORON! :)

My soltion is to rename the 13116785578_addfks.php to 13116785590_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 and doctrine: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 called doctrine:generate-migrations-models.

Conclusion

Another question raises: Why doctrine:generate-migrations-db does not generate migration classes for related tables of the model?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文