简体   繁体   中英

Symfony2 Doctrine schema update fails

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 ?

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

you need to add a value related to the field in the new table.

For example: if you have table A and B, and B is have the key of A (a_id) then you need to add a field in A with id = 1 and in the table B the a_id need to be changed to a value from A table - 1 in this case (make this for all the fields).

After that run : php app/console doctrine:schema:update --force

Regards

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM