Laravel migration (errno: 150 “Foreign key constraint is incorrectly formed”)

后端 未结 18 2463
谎友^
谎友^ 2020-12-05 14:15

I have an orders table and a have a sell_shipping_labels which references orders.id as a foreign. However when I run the Laravel migration I get th

18条回答
  •  独厮守ぢ
    2020-12-05 14:53

    For laravel 6+ users, I agreed with the top 2 answers its all depends on laravel versions, For the latest versions users id column uses big integer. So referencing the users id from current migration you need to use unsignedBigInteger as a reference key. Bellow is a migration example for laravel 6.5.*, Whenever we assign foreign key Keep in mind of your current laravel version

    Schema::create('galleries', function (Blueprint $table) {
            $table->bigIncrements('id');
            ==>$table->unsignedBigInteger('user_id');
            $table->string('title');
            $table->string('description');
            $table->timestamps();
            ==>$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
    

提交回复
热议问题