Symfony2 Doctrine schema update fails

匿名 (未验证) 提交于 2019-12-03 02:56:01

问题:

I created database on my local machine. After moving my project to server I imported backup from local (because I had some important data there).

Now,when I'm trying to update schema on my server it gives my this output:

php app/console doctrine:schema:update --force Updating database schema...        [Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException]                                                                                                                An exception occurred while executing 'ALTER TABLE golf_course ADD CONSTRAINT FK_EC96E162F1503E2B FOREIGN KEY (golf_id) REFERENCES golf (id)':                                  SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`GolfFairway`.`#sql-3fae_7ccf1`, CONSTRAINT `FK_EC9     6E162F1503E2B` FOREIGN KEY (`golf_id`) REFERENCES `golf` (`id`))                                                                                                                      [Doctrine\DBAL\Driver\PDOException]                                                                                                                                             SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`GolfFairway`.`#sql-3fae_7ccf1`, CONSTRAINT `FK_EC9     6E162F1503E2B` FOREIGN KEY (`golf_id`) REFERENCES `golf` (`id`))                                                                                                                      [PDOException]                                                                                                                                                                  SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`GolfFairway`.`#sql-3fae_7ccf1`, CONSTRAINT `FK_EC9     6E162F1503E2B` FOREIGN KEY (`golf_id`) REFERENCES `golf` (`id`))                                                                                                             

Why this happens ? Is there a solution ?

回答1:

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.



回答2:

@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 .



回答3:

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.



回答4:

For me it worked with the following setup (with two relations). The trick was to not mix up mappedBy and inversedBy.

/**  * @ORM\Entity  * @ORM\Table(name="user")  */ class User extends BaseUser {     /**      * @var Merchant      *      * @ORM\OneToOne(targetEntity="Merchant", mappedBy="user")      */     protected $merchant;      /**      * @var Client      *      * @ORM\OneToOne(targetEntity="Client", mappedBy="user")      */     protected $client; }  /**  * @ORM\Table(name="merchant")  * @ORM\Entity  */ class Merchant extends BaseEntity {     /**      * @ORM\OneToOne(targetEntity="User", inversedBy="merchant")      */     protected $user; }  /**  * @ORM\Table(name="client")  * @ORM\Entity  */ class Client extends BaseEntity {     /**      * @ORM\OneToOne(targetEntity="User", inversedBy="client")      */     protected $user; }


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!