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?
I’ve cracked it up.
In a some hilarious reason the auto-generated migration classes are named like this:
The
13116785578_addfks.phpfile appears before1311678582_addproductgroup.php.The file
13116785578_addfks.phpcontaining 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.phpto13116785590_addfks.phpas 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-dbanddoctrine: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-dbonly 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-dbdoes not generate migration classes for related tables of the model?