简体   繁体   English

Symfony2 Doctrine架构更新失败

[英]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 @maxian

Michael Villeneuve answer is not totally right. Michael Villeneuve的回答并不完全正确。 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 . php app / console doctrine:schema:update --dump-sql。 Copy the ouptut. 复制ouptut。 Its the direct SQL queries to update your schema 它是用于更新架构的直接SQL查询
  2. connect mysql with mysql command line or through a mysql client 使用mysql命令行或通过mysql客户端连接mysql
  3. Disable foreign keys checking by call this query : "set foreign_key_checks=0;" 通过调用此查询禁用外键检查:“set foreign_key_checks = 0;”
  4. put the queries from doctrine:schema:update 把来自doctrine的查询:schema:update
  5. Enable back foreign key checking with : "set foreign_key_checks=1;" 启用后退外键检查:“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 如果您在dev中,则可以重建数据库

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. 根据变化,不应超过2-3小时。 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. 如果由于外键约束而导致Doctrine架构更新失败,则只需禁用此特定更新的外键检查。

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; 这个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. app/console doctrine:schema:update --dump-sql的输出app/console doctrine:schema:update --dump-sql所以它实际上调用了Doctrine会调用的东西,但禁用了外键检查。 Configure the mysql call to your needs. 根据您的需要配置mysql调用。

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. 请记住, 有时查询的顺序很重要,而且Doctrine根本没有对它们进行排序。 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). 例如:如果您有表A和B,并且B具有A(a_id)的键,则需要在A中添加id = 1的字段,并且在表B中需要将a_id更改为表格 - 在这种情况下为1(对所有字段都这样)。

After that run : php app/console doctrine:schema:update --force 之后运行: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 . 诀窍是不要混淆mappedByinversedBy

/**
 * @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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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