Back

Having error while using foreign


Akshat Nayak posted 1 week ago

Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table realtimeapp.#sql-1e4_a8 (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table replies add constraint replies_question_id_foreign foreign key (question_id) references questions (id) on delete cascade)

CODE:

<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateRepliesTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('replies', function (Blueprint $table) { $table->bigIncrements('id'); $table->text('body'); $table->integer('question_id')->unsigned(); $table->integer('user_id')->unsigned(); **$table->foreign('question_id')->references('id')->on('questions')->onDelete('cascade');**#when question is deleted delete replies $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('replies'); } }
Tobias van Beek replied 1 week ago

@akshatvishu can you place it next time in a code block, that is more readable :)

What is the type of the questions id column? I see that the question_id is an integer. If the id column on questions is a big integer you need to change the question_id to the same.

Akshat Nayak replied 1 week ago

'id' column on question is this; $table->bigIncrements('id');

Tobias van Beek replied 1 week ago

Then the question_id column need to be:

$table->bigInteger('question_id')->unsigned();

Or short:

$table->unsignedBigInteger('question_id');
Ahmed Sohel replied 1 week ago

or change $table->bigIncrements('id') to $table->increments('id');

Katie Bell replied 5 hours ago

You may don't have the foggiest idea about that numerous site utilizes JavaScript to sidestep SQL articulations, for example, single statement, Hyphen ' - % and so on, this all can be circumvent utilizing Fire Bug or Burp Suite (Data Tampering). So lets take one case here as for example by Coursework Help Consultants and expound it all the more profoundly. As a Good student dependably use Pen-testing lab so here I'm utilizing NOWASP Mutillidae


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us