Max*_*icu 12 php mysql foreign-keys symfony doctrine-orm
我在本地机器上创建了数据库.将我的项目移动到服务器后,我从本地导入备份(因为我有一些重要的数据).
现在,当我尝试更新服务器上的架构时,它会给出我的输出:
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`))
Run Code Online (Sandbox Code Playgroud)
为什么会这样?有解决方案吗?
Cha*_*nel 28
@maxian
Michael Villeneuve的回答并不完全正确.在生产环境或类型的情况下,您只能放弃架构并重新创建它.
在当前架构上执行它的唯一方法是通过以下方式:
我不能保证你不会丢失一些钥匙,但你根本不丢弃你的数据.
Mic*_*uve 10
您的问题是您想要修改具有现有约束的表.我看到两个解决方案:
如果您在dev中,则可以重建数据库
doctrine:database:drop --force
doctrine:database:create
doctrine:schema:create
Run Code Online (Sandbox Code Playgroud)
如果你正在制作它会有点复杂.
我看到的一个解决方案是,您可以创建一个命令来保存数据,删除要更改的表中的数据,修改架构,在更改表后重新加载数据.根据变化,不应超过2-3小时.只要确保你有一个备份,以防你的命令向南.
如果由于外键约束而导致Doctrine架构更新失败,则只需禁用此特定更新的外键检查.
作为一个单行程,您可以运行:
mysql -e "set foreign_key_checks = 0; `app/console doctrine:schema:update --dump-sql`"
Run Code Online (Sandbox Code Playgroud)
这取决于set foreign_key_checks = 0;输出,app/console doctrine:schema:update --dump-sql因此它实际上调用了Doctrine会调用的内容,但禁用了外键检查.根据mysql需要配置呼叫.
您的架构已更新,并且根据您的更改,不会丢失任何数据.
请记住,有时查询的顺序很重要,而且Doctrine根本没有对它们进行排序.在这种情况下,您必须由您自己正确地排序查询,然后使用该有序查询列表.
| 归档时间: |
|
| 查看次数: |
14133 次 |
| 最近记录: |