Home > Mobile >  laravel 8 (errno: 150 "Foreign key constraint is incorrectly formed")
laravel 8 (errno: 150 "Foreign key constraint is incorrectly formed")

Time:11-20

i try to create forum in last php and laravel 8. I have buy course in udemy in laravel 8 i follow video from him but in my computer have error and in video doesn't have

2021_11_13_000535_create_posts_table


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->integer('is_deleted');
            $table->integer('is_approved');
            $table->string('image');
            $table->unsignedBigInteger('discussion_id');
            $table->foreign('discussion_id')->references('id')->on('discussions')->onDelete('cascade');
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->string('slug');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

2021_11_19_165302_create_discussions_table

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateDiscussionsTable extends Migration
{
   /**
    * Run the migrations.
    *
    * @return void
    */
   public function up()
   {
       Schema::create('discussions', function (Blueprint $table) {
           $table->id();
           $table->string('title');
           $table->string('desc');
           $table->unsignedBigInteger('forum_id');
           $table->foreign('forum_id')->references('id')->on('forums')->onDelete('cascade');
           $table->integer('is_deleted')->default(0);
           $table->string('image')->nullable();
           $table->integer('notify')->default(0);
           $table->unsignedBigInteger('user_id');
           $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

           $table->timestamps();
       });
   }

   /**
    * Reverse the migrations.
    *
    * @return void
    */
   public function down()
   {
       Schema::dropIfExists('discussions');
   }
}

when i try to do migrate table i have this error :

Migrated:  2014_10_12_000000_create_users_table (1,399.45ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (2,117.91ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (1,592.76ms)
Migrating: 2019_12_14_000001_create_personal_access_tokens_table
Migrated:  2019_12_14_000001_create_personal_access_tokens_table (2,125.96ms)
Migrating: 2021_11_12_234608_create_categories_table
Migrated:  2021_11_12_234608_create_categories_table (2,452.77ms)
Migrating: 2021_11_12_235039_create_forums_table
Migrated:  2021_11_12_235039_create_forums_table (2,849.71ms)
Migrating: 2021_11_13_000340_create_tags_table
Migrated:  2021_11_13_000340_create_tags_table (526.62ms)
Migrating: 2021_11_13_000535_create_posts_table

   Illuminate\Database\QueryException 

  SQLSTATE[HY000]: General error: 1005 Can't create table `stsdb`.`posts` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `posts` add constraint `posts_discussion_id_foreign` foreign key (`discussion_id`) references `discussions` (`id`) on delete cascade)

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:703
    699         // If an exception occurs when attempting to run a query, we'll format the error
    700         // message to include the bindings with SQL, which will make this exception a
    701         // lot more helpful to the developer instead of just the database's errors.
    702         catch (Exception $e) {
   703             throw new QueryException(
    704                 $query, $this->prepareBindings($bindings), $e
    705             );
    706         }
    707     }

       9 vendor frames 
  10  database/migrations/2021_11_13_000535_create_posts_table.php:28
      Illuminate\Support\Facades\Facade::__callStatic()

       21 vendor frames 
  32  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()
 

someone can explain why have this error and how resolve this? because i dont understand i follow like in video in my pc have error and in video doesn't have. Someone can help resolve this? I have checked in google but i have dont find how resolve this, i follow tuts build forum laravel 8 with telegram notification

CodePudding user response:

The problem is that your migration for the posts table is run prior to your discussions migration.

This happens because Laravel runs the migrations ordered by the timestamp in the migrations file name:

2021_11_13_000535_create_posts_table -> 13. November
2021_11_19_165302_create_discussions_table -> 19. November

Therefore the dicsussions table is not created yet as my comment suggested!

The solution is easy, change the file name to:
2021_11_20_000535_create_posts_table -> 20. November

Next time please take a look into your DB as I suggested earlier.

From their documentation:

Generating Migrations

You may use the make:migration Artisan command to generate a database migration. The new migration will be placed in your database/migrations directory. Each migration filename contains a timestamp that allows Laravel to determine the order of the migrations:

https://laravel.com/docs/8.x/migrations#generating-migrations

  • Related