Php 外键约束的格式不正确。你能帮助我吗?

Php 外键约束的格式不正确。你能帮助我吗?,php,mysql,laravel,laravel-5,migration,Php,Mysql,Laravel,Laravel 5,Migration,你能帮我吗 我使用MySQL。 我做 php artisan migrate 但是错误 Illumb\Database\QueryException:SQLSTATE[HY000]:一般错误: 1005无法创建表学习产品(错误号:150“外键 约束格式不正确”)(SQL:alter tableproductsadd 约束products\u category\u id\u foreign外键(category\u id) 参考资料类别(id) 类别迁移: Schema::cre

你能帮我吗

我使用MySQL。

我做

php artisan migrate
但是错误

Illumb\Database\QueryException:SQLSTATE[HY000]:一般错误: 1005无法创建表
学习
产品
(错误号:150“外键
约束格式不正确”)(SQL:alter table
products
add 约束
products\u category\u id\u foreign
外键(
category\u id
) 参考资料
类别
id

类别
迁移:

        Schema::create('categories', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name', 50)->unique();
        $table->timestamps();
        $table->softDeletes();
    });
    Schema::create('products', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name', 150)->unique();
        $table->unsignedBigInteger('category_id');
        $table->string('image', 255);
        $table->boolean('sale')->default(false);
        $table->text('description');
        $table->timestamps();
        $table->softDeletes();
        $table->foreign('category_id')->references('id')->on('categories');
    });
产品
迁移:

        Schema::create('categories', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name', 50)->unique();
        $table->timestamps();
        $table->softDeletes();
    });
    Schema::create('products', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name', 150)->unique();
        $table->unsignedBigInteger('category_id');
        $table->string('image', 255);
        $table->boolean('sale')->default(false);
        $table->text('description');
        $table->timestamps();
        $table->softDeletes();
        $table->foreign('category_id')->references('id')->on('categories');
    });

替换:
$table->unsignedbiginger('category_id')

使用:
$table->integer('category_id')->unsigned()

替换:
$table->increments('id')

使用:
$table->bigIncrements('id')

在两个表中(但至少在类别表中)


为什么?:

从MySQL文档:

外键和引用键中的相应列必须具有相似的数据类型整数类型的大小和符号必须相同。字符串类型的长度不必相同。对于非二进制(字符)字符串列,字符集和排序规则必须相同

从Laravel文档:

$table->increments():自动递增无符号整数(主键)等效列

$table->unsignedbiginger():UNSIGNED BIGINT等效列


因此,在你的情况下:

$table->unsignedbiginger('category_id')=>无符号BIGINT

$table->increments('id')=>无符号整数


它们不匹配。

这是因为
产品.category\u id
列和
categories.id
列之间的类型不匹配。您需要使您的
id
大增量
,或者将您的
category\u id
设置为
->整数('category\u id')->无符号()
非常感谢您的可能副本。非常描述性的回答。