【问题标题】:Laravel SQLSTATE[23000] Cannot delete or update a parent row: a foreign key constraint failsLaravel SQLSTATE[23000] 无法删除或更新父行:外键约束失败
【发布时间】:2021-12-14 02:54:00
【问题描述】:

我希望你没事,我只是想发布这篇文章,因为 down() 函数和删除列以及所有这些东西对我也不起作用。

例如:

public function down()
{
    Schema::table('users', function($table)
    {
        $table->dropColumn(array('description1', 'description2', 'description3'));
    });
}

来自这篇文章:Laravel migrate:rollback adding and deleting table columns

【问题讨论】:

    标签: php mysql laravel foreign-keys laravel-artisan


    【解决方案1】:

    我编写了一个简单的命令行来按顺序删除所有表。

    代码:

    Artisan::command('delete-all', function() {
        (Schema::dropIfExists('bloggers'));
        (Schema::dropIfExists('ticket_labels'));
        (Schema::dropIfExists('labels'));
        (Schema::dropIfExists('tickets'));
        (Schema::dropIfExists('service_admins'));
        (Schema::dropIfExists('services'));
        (Schema::dropIfExists('admins'));
        (Schema::dropIfExists('admin_roles'));
        (Schema::dropIfExists('priorities'));
        (Schema::dropIfExists('statuses'));
        (Schema::dropIfExists('users'));
        (Schema::dropIfExists('failed_jobs'));
        (Schema::dropIfExists('migrations'));
        (Schema::dropIfExists('password_resets'));
        (Schema::dropIfExists('personal_access_tokens'));
        // After deleting all the tables, now executing migrate command-line.
        exec('php artisan migrate');
        $this->comment('Done');
    })->purpose('Deleting all the tables on database');
    

    我希望你喜欢:)。 享受吧。

    【讨论】:

      【解决方案2】:

      问题可能是您正在尝试应用适用于旧版本 Laravel 的解决方案,如果您正在使用新版本,最好使用方括号来定义数组是什么。

      public function down() {
          Schema::table('users', function($table) {
              $table->dropColumn(['description1', 'description2', 'description3']);
          });
      }
      

      我建议你检查你正在使用的 laravel 的版本,并根据版本寻找答案。

      【讨论】:

        猜你喜欢
        • 2017-07-20
        • 2019-05-25
        • 2018-05-12
        • 1970-01-01
        • 2017-06-11
        • 1970-01-01
        • 2014-11-11
        • 2017-09-15
        相关资源
        最近更新 更多