为什么 Laravel 5.8 迁移在不更改 table 名称的情况下不起作用?

Why does Laravel 5.8 migration not work without changing the table name?

我有以下 table 架构;

Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email');
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->string('account_type')->default('1,1,0');
            $table->string('theme')->default('light');
            $table->string('unique_id')->nullable();
            $table->string('avatar')->nullable();
            $table->rememberToken();
            $table->timestamps();
            $table->unsignedBigInteger('plan_id');
            $table->foreign('plan_id')->references('id')->on('plans');
            $table->unique('email');
        });

当我 运行 迁移时,它失败并显示错误:

errno: 150 "Foreign key constraint is incorrectly formed"

如果我删除外键,它仍然不起作用。

它确实有效,如果我将 table 名称从 'users' 更改为其他任何名称,但如果我再次 运行 它就会失败(需要 table名称将再次更改)。

这是引用的 table 的架构,此迁移也在用户迁移之前 运行s。

Schema::create('plans', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name')->unique();
            $table->float('cost', 8, 2)->nullable();
            $table->integer('free_trial');
            $table->string('renewal_rate');
            $table->string('features');
            $table->string('stripe_plan');
            $table->boolean('featured')->default(true);
            $table->string('permission');
            $table->timestamps();
        });

花了几天时间试图解决这个问题,每个人似乎都指出 bigIncrements/unsignedBigIncrements 与列名中的错别字相同。我好像没有这个问题..

您想在计划 table 之前创建 users table。这就是您收到此错误的原因。首先,您需要创建没有 'plan_id' 的用户 table:

Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email');
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->string('account_type')->default('1,1,0');
            $table->string('theme')->default('light');
            $table->string('unique_id')->nullable();
            $table->string('avatar')->nullable();
            $table->rememberToken();
            $table->timestamps();
            $table->unique('email');
        });

然后迁移 table(usersplans)。迁移后,您需要创建新的迁移。

在控制台上写:php artisan make:migration add_plan_id_to_users --table=users

然后将其添加到您的新迁移中:

$table->unsignedBigInteger('plan_id');
$table->foreign('plan_id')->references('id')->on('plans');

这似乎是 XAMPP 的问题,重新安装 XAMPP 已解决问题,我不确定原因,它还减少了从大约 5 开始迁移所需的时间每 table -10 秒到每 table.

0.1-0.6