Laravel 5.8为何不更改表名就无法工作?

问题描述 投票:1回答:2

我有以下表模式;

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“外键约束格式不正确”

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

[如果我将表名从'users'更改为其他名称,它确实起作用,但是如果我再次运行它(要求再次更改表名),它将失败。

这里是所引用表的架构,该迁移也将在用户迁移之前运行。

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与列名中的错字相同。我似乎没有那个问题。

mysql sql laravel laravel-5.8
2个回答
0
投票
您想在计划users之前创建table表。这就是为什么您会收到此错误。首先,您需要创建不带'plan_id'的用户表:

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'); });

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

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

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

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


0
投票
这似乎是XAMPP的问题,重新安装XAMPP已解决了该问题,我不确定原因,它也将迁移时间从每张表5-10秒减少到每张表0.1-0.6 。
© www.soinside.com 2019 - 2024. All rights reserved.