Symfony2 Doctrine schema update fails

@maxian

Michael Villeneuve answer is not totally right. In case of a production environnement or kind of , you just can t drop schema and recreate it.

The only way to perform it on your current schema is by the followings :

  1. php app/console doctrine:schema:update --dump-sql . Copy the ouptut. Its the direct SQL queries to update your schema
  2. connect mysql with mysql command line or through a mysql client
  3. Disable foreign keys checking by call this query : "set foreign_key_checks=0;"
  4. put the queries from doctrine:schema:update
  5. Enable back foreign key checking with : "set foreign_key_checks=1;"

i cannot guarantee you won t lost some keys but you don t drop your datas at all .


Your problem is that you want to modify a table with existing constraint. I see two solutions:

If you are in dev, you can rebuild your database

doctrine:database:drop --force
doctrine:database:create
doctrine:schema:create

If you're in production it's a little more complicated.

One solution I see is that you could create a command to save your data, delete the data in the tables you want to alter, modify your schema, reload the data once your table is altered. Depending on the changes, it shouldn't take more then 2-3 hours. Just make sure you have a backup in case your command goes south.


Schema update with foreign-key checks disabled

If Doctrine schema updates fail because of foreign-key constraints, you simply need to disable foreign-key checks for this particular update.

As a one-liner you can run:

mysql -e "set foreign_key_checks = 0; `app/console doctrine:schema:update --dump-sql`"

This prepends set foreign_key_checks = 0; to the output of app/console doctrine:schema:update --dump-sql so it actually calls exactly what Doctrine would call but with foreign-key checks disabled. Configure the mysql call to your needs.

Your schema is updated and depending on your changes no data is lost.

Keep in mind that sometimes the order of the queries is important and Doctrine simply didn't order them right. In this case you have to order the queries correctly by your own and then use that ordered list of queries instead.