Laravel (errno: 150 "Foreign key constraint is incorrectly formed")

-1

I do not know what happens.

Schema::create('option_question', function (Blueprint $table) {
    $table->increments('id');
    $table->unsignedInteger('question_id');
    $table->unsignedInteger('option_id');
    $table->timestamps();
});

Schema::create('questions', function(Blueprint $table)
{
    $table->increments('id');
    $table->text('text');
    $table->integer('type');
    $table->string('short_text')->nullable();
    $table->integer('orden');
    $table->integer('status');
    $table->timestamps();
});

Schema::table('questions', function (Blueprint $table) {
    $table->foreign('id')
        ->references('question_id')
        ->on('option_question')
        ->onDelete('no action');
});
    
asked by Franklin'j Gil'z 28.06.2017 в 17:58
source

1 answer

1

I think you're raising everything the other way around, is that possible? Bearing in mind that you intend to start from a kind of pivot table and create the primary keys of the other tables from this one.

Schema::create('questions', function(Blueprint $table) {
    $table->increments('id');
    $table->text('text');
    $table->integer('type');
    $table->string('short_text')->nullable();
    $table->integer('orden');
    $table->integer('status');
    $table->timestamps();
});

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

Schema::create('option_question', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('option_id')->unsigned()->index();
    $table->foreign('option_id')->references('id')->on('options')->onDelete('cascade');
    $table->integer('question_id')->unsigned()->index();
    $table->foreign('question_id')->references('id')->on('questions')->onDelete('cascade');
    $table->timestamps();

});
    
answered by 28.06.2017 в 18:10