0

so in my Tasks migration table I'm trying to reference a foriegn key constraint

Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table tasks add constraint tasks_article_id_foreign foreign key (article_id) references articles (id) on delete cascade)

I've followed up on google regarding the error, it says to use unsignedBigInteger for bigIncrements reference. I did that but still I get this error:

I followed this link: Laravel Migration Foreign key constraint is incorrectly formed

Can you guys point me in the right direction?

    {
        Schema::create('tasks', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->string('task_url');
            $table->string('task_unique_id');
            $table->unsignedBigInteger('user_id')->default(1);
            $table->unsignedBigInteger('article_id')->nullable();
            $table->unsignedBigInteger('project_id')->nullable();
            $table->unsignedBigInteger('category_id')->nullable();
            $table->boolean('is_completed')->default(0);
            $table->timestamps();


            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
            $table->foreign('project_id')->references('id')->on('projects')->onDelete('cascade');
            $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');

        });
    }
Rohan Shukla
  • 89
  • 1
  • 12

1 Answers1

0

You can disable the foreign key checks before the create method and re enable afterwards.

{
    Schema::disableForeignKeyConstraints();
    Schema::create('tasks', function (Blueprint $table) {
        ....
    });
    Schema::enableForeignKeyConstraints();
}

Michael Mano
  • 3,339
  • 2
  • 14
  • 35