laravel migration best way to add foreign key

后端 未结 10 1068
梦毁少年i
梦毁少年i 2020-12-09 09:19

Simple question: I\'m new to Laravel. I have this migration file:

Schema::create(\'lists\', function(Blueprint $table) {
    $table->increments(\'id\'); 
         


        
10条回答
  •  独厮守ぢ
    2020-12-09 09:59

    I was doing the same but got error " id not exist" => so I changed my migration file as below :

    question table migration content:

    $table->id() => should change to $table->increments('id')

    definitions of foreign key in Reply table:

    $table->foreign('question_id')->references('id')->on('questions')->onDelete('cascade');

    now your foreign key will work.

提交回复
热议问题