【问题标题】:Why does Laravel 5.8 migration not work without changing the table name?为什么 Laravel 5.8 在不更改表名的情况下迁移不起作用?
【发布时间】:2019-12-22 21:15:52
【问题描述】:

我有以下表架构;

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

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

如果我将表名从“用户”更改为其他任何名称,它确实有效,但如果我再次运行它会失败(需要再次更改表名)。

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

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 与列名中的拼写错误相同。我好像没有这个问题。。

【问题讨论】:

  • 所以如果你使用“asdasdasd”之类的用户而不是它,它可以工作吗?
  • users 将无法工作,如果我更改表名一次,它会工作,但它会再次中断,并出现上述错误。我希望表名是用户。

标签: mysql sql laravel laravel-5.8


【解决方案1】:

您想在计划table 之前创建users 表。这就是您收到此错误的原因。首先你需要创建没有'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');

【讨论】:

  • 我在计划之后创建了用户表,因为计划有外键 - 这是错误的吗?那么这样做,我需要为每个外部约束单独迁移吗?
  • 如果您在plans 之后创建了users 表,则无需单独迁移。注意:用户表在 Laravel 包中首先作为默认值给出
  • 我已经根据计划创建了 users 表,这也是 Laravel 默认包中自定义的 users 表。奇怪的是,如果我将 users 更改为任何内容,例如 users2 进行一次迁移,它就会起作用。但是如果我不得不php artisan migrate:fresh
  • 要补充的是,我现在尝试使用新数据库运行它 - users 表现在可以正确传递,但看似随机的表将失败,errno: 13 "Permission denied" 尽管存在正确的权限(并且它在失败之前已经创建了几个表)。
【解决方案2】:

这似乎是 XAMPP 的问题,重新安装 XAMPP 已解决该问题,我不确定原因,它还将迁移所需的时间从每个表大约 5-10 秒减少到每个表 0.1-0.6 秒表。

【讨论】:

    猜你喜欢
    • 2023-03-19
    • 2021-04-22
    • 1970-01-01
    • 2015-05-26
    • 2019-11-15
    • 2019-11-23
    • 2020-02-20
    • 1970-01-01
    • 2020-05-11
    相关资源
    最近更新 更多