Symfony 1.4 migration throws SQLSTATE [HY000]: General error: 1005 (err 150) - doctrine:migrate fails!
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?
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
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?
精彩评论