【问题标题】:Can't create table `clothing`.`clothes` (errno: 150 "Foreign key constraint is incorrectly formed")") Laravel 7无法创建表`clothing`.`clothes`(errno:150“外键约束形成错误”)“)Laravel 7
【发布时间】:2021-01-05 16:20:31
【问题描述】:

无法创建表clothing.clothes (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');
        });
    }

【问题讨论】:

    标签: php mysql laravel foreign-keys backend


    【解决方案1】:

    这是正确的答案 谢谢!

    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');
    

    【讨论】:

    • 您想解释一下您所做的更改,这样人们就不必比较文字墙了吗?
    猜你喜欢
    • 2021-12-24
    • 2020-12-19
    • 2021-08-06
    • 2021-07-27
    • 2021-07-22
    • 2020-12-02
    • 2022-01-25
    • 2018-02-04
    • 2020-07-05
    相关资源
    最近更新 更多