首页 > 解决方案 > 无法创建表`clothing`.`clothes`(errno:150“外键约束形成错误”)“)Laravel 7

问题描述

无法创建表clothingclothes(errno: 150 "外键约束格式不正确")") Laravel 7

不能断言外键。

乐队

   Schema::create('bands', function (Blueprint $table) {
        $table->id();
        $table->string('name');

        $table->string('band_origin');
        $table->text('band_details');
        $table->timestamps();
        $table->softDeletes();
    });

类别

Schema::create('category', function (Blueprint $table) {
            $table->id();
            $table->string('category_name');
            $table->string('category_description');
            $table->timestamps();
            $table->softDeletes(); 


**gender**

Schema::create('gender', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('description');
            $table->timestamps();
            $table->softDeletes();


**colors**

Schema::create('colors', function (Blueprint $table) {
            $table->id('idColor');
            $table->String('color_name');
            $table->String('color_description')->nullable();
            $table->timestamps();
            $table->softDeletes();
        });



**ERROR HERE TABLE CLOTHES WHERE I WANT to ACQUIRE those field in this table**


   Schema::create('clothes', function (Blueprint $table) {
            $table->id('clothesID'); //PK
            $table->bigInteger('bandID_fk')->unsigned();
            $table->bigInteger('categoryID_fk')->unsigned();
            $table->bigInteger('genderID_fk')->unsigned();
            $table->bigInteger('colorID_fk')->unsigned();
            $table->tinyInteger('onStock');
            $table->integer('price');
            $table->integer('discount')->default(0);
            $table->string('description')->nullable();
            $table->integer('shoeSize')->nullable();
            $table->integer('stock');
            $table->string('photo1');
            $table->string('photo2')->nullable();
            $table->string('photo3')->nullable();
            $table->timestamps();
            $table->softDeletes();

//            ERROR 
            $table->foreign('bandID_fk')->references('id')->on('bands');
            $table->foreign('categoryID_fk')->references('id')->on('category');
            $table->foreign('genderID_fk')->references('id')->on('gender');
            $table->foreign('colorID_fk')->references('idColor')->on('users');
        });
    }

标签: phpmysqllaravelforeign-keysbackend

解决方案


这是正确的答案 谢谢!

Schema::create('clothes', function
    (Blueprint $table) {
        $table->id('clothesID'); //PK
        $table->bigInteger('bandID_fk')->unsigned();
        $table->bigInteger('categoryID_fk')->unsigned();
        $table->bigInteger('genderID_fk')->unsigned();
        $table->bigInteger('colorID_fk')->unsigned();
        $table->tinyInteger('onStock');
        $table->integer('price');
        $table->integer('discount')->default(0);
        $table->string('description')->nullable();
        $table->integer('shoeSize')->nullable();
        $table->integer('stock');
        $table->string('photo1');
        $table->string('photo2')->nullable();
        $table->string('photo3')->nullable();
        $table->timestamps();
        $table->softDeletes();


        $table->foreign('bandID_fk')->references('id')->on('bands');
        $table->foreign('categoryID_fk')->references('id')->on('category');
        $table->foreign('genderID_fk')->references('id')->on('gender');
        $table->foreign('colorID_fk')->references('idColor')->on('colors');

推荐阅读