使用迁移删除具有外键的表 [英] Using migrations to delete table with foreign key

查看:156
本文介绍了使用迁移删除具有外键的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试回滚迁移.

我的迁移文件像这样使用外键

My migrations file uses foreign keys like so

$table->foreign('user_one')->references('id')->on('users');
$table->foreign('user_two')->references('id')->on('users');

我的down()函数就像这样

My down() function is like so

public function down()
{
    Schema::drop('pm_convo');
    Schema::drop('pm_convo_replys');
}

当我运行迁移命令时

php artisan migrate:refresh --seed --env=local

我遇到以下错误

SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key constraint fails (SQL: drop table `pm_convo`) 

我不确定是否要解决此问题.

Im not exactly sure what to do to fix this.

我尝试过:$table->dropForeign('pm_convo_user_one_foreign');

但与此同时我也遇到了错误

But im getting errors with that as well

推荐答案

pm_convo_replys具有引用pm_convo的外键,因此您在不违反外键的情况下不能先删除pm_convo pm_convo_replys中的约束.

pm_convo_replys has a foreign key that references pm_convo, thus you cannot delete pm_convo first without violating a foreign key constraint in pm_convo_replys.

要同时删除两者,您需要先删除pm_convo_replys .

To delete both you need to delete pm_convo_replys first.

public function down()
{
    Schema::drop('pm_convo_replys');
    Schema::drop('pm_convo');
}

这篇关于使用迁移删除具有外键的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆