Laravel数据透视表生成不起作用(返回一般错误:1005无法创建表)

时间:2019-08-19 03:12:25

标签: laravel

我正在尝试建立数据透视表,以便在酒店中存储其他物品。为此,

我首先创建的酒店表

    Schema::create('hotels', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->text('description');
        $table->string('photo');
        $table->tinyInteger('status')->comment('1 - enable, 0 - disable');
        $table->softDeletes();
        $table->timestamps();
    });

然后我创建了extra_items表

    Schema::create('extra_items', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->timestamps();
    });

我还创建了货币表

    Schema::create('currencies', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->tinyInteger('default')->default(0)->comment('0 - not default, 1 - default');
        $table->timestamps();
    });

最后,我尝试创建数据透视表,但是当我尝试迁移时,它抛出了以下错误,

  

Illuminate \ Database \ QueryException:SQLSTATE [HY000]:常规   错误:1005无法创建表hotels#sql-3038_ab(错误号:150   “外键约束格式不正确”)(SQL:alter table   extra_item_hotel添加约束   extra_item_hotel_extra_item_id_foreign外键(extra_item_id)   引用extra_itemsid))

这就是我创建迁移文件的方式,

    Schema::create('extra_item_hotel', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedInteger('extra_item_id');
        $table->unsignedInteger('hotel_id');
        $table->unsignedInteger('currency_id');
        $table->tinyInteger('status')->default(1)->comment('0 - disable, 1 - enable');
        $table->softDeletes();
        $table->timestamps();

        $table->foreign('hotel_id')->references('id')->on('hotels');
        $table->foreign('extra_item_id')->references('id')->on('extra_items');
        $table->foreign('currency_id')->references('id')->on('currencies');

    });

实际上我找不到错误的原因,供我参考,我创建了类似的类型数据透视表,而该项目中没有任何问题,例如

    Schema::create('hotel_meal_plan', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedBigInteger('hotel_id');
        $table->foreign('hotel_id')->references('id')->on('hotels');
        $table->unsignedBigInteger('meal_plan_id');
        $table->foreign('meal_plan_id')->references('id')->on('meal_plans');
        $table->unsignedBigInteger('currency_id');
        $table->foreign('currency_id')->references('id')->on('currencies');
        $table->float('amount');
        $table->softDeletes();
        $table->timestamps();
    });

有人可以帮助我找到问题,谢谢。

1 个答案:

答案 0 :(得分:0)

外键列和引用列应为同一类型。

HERE是一篇很好的文章,解释了与您类似的情况。