使用 laravel 中的外键迁移时出错

error while migrating with foreign key in laravel

我正在尝试迁移下面的函数,但它每次都会给我同样的错误,我没能找出原因,即使我尝试了文档中的其他语法

//向上函数

public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->id('cat_id');
            $table->string('cat_name');
            $table->string('cat_desc');
            $table->string('cat_image');
            $table->timestamps();
        });



Schema::create('sellers', function (Blueprint $table) {
        $table->id('seller_id');
        $table->string('seller_name');
        $table->string('seller_desc');
        $table->string('seller_image');
        $table->timestamps();
    });

    Schema::create('products', function (Blueprint $table) {
        $table->id('prd_id');
        $table->string('prd_name');
        $table->string('prd_price');
        $table->string('prd_image');
        $table->string('prd_desc');
        $table->foreignId('product_cat_id')->constrained('categories');
        $table->foreignId('product_seller_id')->constrained('sellers');
        $table->boolean('prd_status');
        $table->timestamps();
    });

}

//错误

SQLSTATE[HY000]: General error: 1005 Can't create table ecommerce.products (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table products add constraint products_product_cat_id_foreign foreign key (product_cat_id) references categories (id))

我也从数据库中删除了迁移文件并进行了新的迁移,但没有帮助,请指导我,在此先感谢

public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('cat_id')->unsigned();
            $table->string('cat_name');
            $table->string('cat_desc');
            $table->string('cat_image');
            $table->timestamps();
        });



Schema::create('sellers', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('seller_id')->unsigned();
        $table->string('seller_name');
        $table->string('seller_desc');
        $table->string('seller_image');
        $table->timestamps();
    });

    Schema::create('products', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('prd_id')->unsigned();
        $table->string('prd_name');
        $table->string('prd_price');
        $table->string('prd_image');
        $table->string('prd_desc');
        $table->foreignId('product_cat_id')->constrained('categories');
        $table->foreignId('product_seller_id')->constrained('sellers');
        $table->boolean('prd_status');
        $table->timestamps();
    });

}

这是因为,在您的 categoriessellers 上,默认情况下 table 主键 都不是 id constrained 方法使用 id 作为外键引用的主键,所以更改:

$table->foreignId('product_cat_id')->constrained('categories');
$table->foreignId('product_seller_id')->constrained('sellers');

到,

$table->unsignedBigInteger('product_cat_id');
$table->foreign('product_cat_id')->references('cat_id')->on('categories');

$table->unsignedBigInteger('product_seller_id');
$table->foreign('product_seller_id')->references('seller_id')->on('sellers');

如果您使用的是模型,那么还要在您的模型上定义 primary key,例如:

protected $primaryKey = 'cat_id';