gpt4 book ai didi

laravel - 使用迁移删除带有外键的表

转载 作者:行者123 更新时间:2023-12-02 14:10:31 24 4
gpt4 key购买 nike

我正在尝试回滚我的迁移。

我的迁移文件使用外键,如下所示:

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

我的 down() 函数如下所示:

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`) 

我不太确定如何解决这个问题。

我已经尝试过:$table->dropForeign('pm_convo_user_one_foreign');,但我也遇到了错误。

最佳答案

我认为这是一个更好的方法:

public function down()
{
DB::statement('SET FOREIGN_KEY_CHECKS = 0');
Schema::dropIfExists('tableName');
DB::statement('SET FOREIGN_KEY_CHECKS = 1');
}

关于laravel - 使用迁移删除带有外键的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17108671/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com