即使我 migrate:fresh 它告诉我 table 已经存在.. 为什么?

Even if i migrate:fresh it shows me that the table already exists.. why?

所以我想 php artisan migrate:fresh 但我得到这个错误

Base table or view already exists: 1050 Table 'roles' already exists

即使我从 phpmyadmin 中删除数据库,清理缓存并再次创建数据库,它仍然显示相同的消息 table 行的迁移如下:

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateRolesTable extends Migration
{
    public function up()
    {
        Schema::create('roles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->tinyInteger('status');
        });
    }

    public function down()
    {
        Schema::dropIfExists('roles');
    }
}

显示的完整错误:

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'roles' already exists (SQL: create table roles (id bigint unsigned not null auto_increment primary key, name varchar(255) not null, guard_name varchar(255) not null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

这是为什么?我可以或应该做什么?

如果它在您 artisan 迁移之前已经存在,它当然会这样说。您是否进行了迁移,但它在中途中断了?您可以重置它或只删除 table 然后重试(如果您在本地开发中并且可以删除它)。

php artisan migrate:reset

首先使用此代码删除 角色 table Schema::dropIfExists('roles'); 然后创建。

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateRolesTable extends Migration
{
    public function up()
    {
        Schema::dropIfExists('roles'); //added
        Schema::create('roles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->tinyInteger('status');
        });
    }

    public function down()
    {
        Schema::dropIfExists('roles');
    }
}