错误:150“外键约束格式错误”我该如何解决?

时间:2019-09-12 15:09:32

标签: php mysql laravel migration

我一直在尝试不同的方法,但是我无法摆脱错误。所以我的问题是:别人能看到错误了吗?

这是我的代码:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id')->unique();
        $table->boolean('admin')->default('0');
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}


public function up()
{
    Schema::create('places', function (Blueprint $table) {
        $table->bigIncrements('id')->unique();
        $table->string('location_name');
        $table->string('village');
        $table->string('street');
        $table->integer('number')->unsigned();
    });
}

public function up()
{
    Schema::create('planning', function (Blueprint $table) {
        $table->bigIncrements('id')->unique();
        $table->unsignedInteger('places_id');
        $table->time('van');
        $table->time('tot');
        $table->date('dag');
        $table->timestamps();
        $table->unsignedInteger('created_by');

        $table->foreign('places_id')
            ->references('id')
            ->on('places')
            ->onDelete('cascade');

        $table->foreign('created_by')
            ->references('id')
            ->on('users')
            ->onDelete('cascade');
    });

}
  

PDOException::(“ SQLSTATE [HY000]:常规错误:1005无法创建   表`foodtruck`。`#sql-3be8_b8`(errno:150“外键约束   格式不正确”“)”)

我希望此错误消息离开我的命令行,并以一种我可以正常使用它的方式来修复我的迁移文件:)

1 个答案:

答案 0 :(得分:1)

由于places_idcreated_by被定义为bigIncrements,因此您无法将它们的外键定义为unsignedInteger,因此它必须是根据{{ 3}}是:

  

自动递增UNSIGNED BIGINT(主键)等效列。

等效于unsignedBigInteger

更改

$table->unsignedInteger('places_id');
$table->unsignedInteger('created_by');

收件人,

$table->unsignedBigInteger('places_id');
$table->unsignedBigInteger('created_by');