使用不存在的迁移进行 Laravel 迁移

bar*_*uck 15 migration laravel

我正在尝试进行非常基本的迁移。当我运行 php artisan migrate 时出现此错误:

Migrating: 2019_12_14_000001_create_personal_access_tokens_table
 Illuminate\Database\QueryException 
  SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'personal_access_tokens' already exists (SQL: create tab....///
Run Code Online (Sandbox Code Playgroud)

如您所见,我的迁移表中没有“2019_12_14_000001_create_personal_access_tokens_table”迁移。

在此输入图像描述

我做了以下事情:

php artisan cache:clear
php artisan config:clear
./composer dump-autoload
Run Code Online (Sandbox Code Playgroud)

我仍然在经历这种幽灵般的迁徙。我应该怎么办?

我尝试运行的 create_file_table 文件的内容是:

 public function up()
{
    Schema::create('files', function (Blueprint $table) {
        $table->id();
        $table->string('quoteNumber');
        $table->string('purchaseOrderNumber');
        $table->string('name')->nullable();
        $table->string('file_path')->nullable();
        $table->timestamps();
    });
}
Run Code Online (Sandbox Code Playgroud)

who*_*boy 20

2019_12_14_000001_create_personal_access_tokens_table
Run Code Online (Sandbox Code Playgroud)

Laravel Sanctum的一部分。

您可以通过运行以下命令将其发布到迁移文件夹。

php artisan vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
Run Code Online (Sandbox Code Playgroud)

否则它会保留在包中,并且您将不会在迁移文件夹中看到它。

如果您想忽略它,可以按照下面包含的文档进行操作。

迁移定制

如果您不打算使用 Sanctum 的默认迁移,则应该Sanctum::ignoreMigrations在类register的方法中调用该方法App\Providers\AppServiceProvider。您可以通过执行以下命令导出默认迁移:

php artisan vendor:publish --tag=sanctum-migrations

  • 如果您不使用“composer remove laravel/sanctum”,您也可以删除 Sanctum (3认同)
  • 要添加到 @DisgruntledGoat 的评论 - 删除 Sanctum 后,删除位于 `config/sanctum.php` 的配置文件,因为它将尝试导入 Sanctum 的类并导致错误。 (2认同)

mys*_*dad -2

正如错误所示,该表存在。

php artisan cache:clear
php artisan config:clear
./composer dump-autoload
Run Code Online (Sandbox Code Playgroud)

不管用。为什么?因为 laravel 检查表migration而不是缓存或配置。

在这些解决方案中,您有:

  1. artisan migrate:fresh这将从数据库中删除所有表,然后执行迁移命令
  2. migrations表中,手动删除迁移记录。并删除表的实例。

您共享的屏幕截图来自代码,而不是数据库。如果您引用数据库,您将找到具有该名称的记录。

为什么会出现这种情况?

迁移是分批进行的。当表创建后删除迁移文件时,该表仍然存在。适当的方法是回滚该迁移。因此,文件不存在并不意味着表不存在。