ankush981
ankush981

Reputation: 5417

Laravel migration 150 error on MySQL

I've already seen other questions related to this error on Laravel migrations, but couldn't find a solution. Maybe I'm missing something fundamental.

In order, my migrations are:

A category has many sub_categories, which has many services.

So here's what I wrote in my migration files:

Categories:

class CreateCategoriesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();

            $table->string('name', 100);
        });
    }

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

Sub-Categories:

class CreateSubCategoriesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('sub_categories', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();

            $table->string('name', 100);
            $table->integer('category_id')->unsigned();

            $table->foreign('category_id')->references('categories')
                    ->on('id')->onDelete('cascade');
        });
    }

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

Services:

class CreateServicesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('services', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();

            $table->string('name', 100);
            $table->integer('sub_category_id')->unsigned();

            $table->foreign('sub_category_id')->references('sub_categories')
                    ->on('id')->onDelete('cascade');
        });
    }

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

On php artisan migrate, I get the following:

 [Illuminate\Database\QueryException]                                                                     
  SQLSTATE[HY000]: General error: 1005 Can't create table 'servoapp.#sql-b79_34' (errno: 150) (SQL: alter  
   table `sub_categories` add constraint `sub_categories_category_id_foreign` foreign key (`category_id`)  
   references `id` (`categories`) on delete cascade)                                                       

 [PDOException]                                                                               
  SQLSTATE[HY000]: General error: 1005 Can't create table 'servoapp.#sql-b79_34' (errno: 150)  

I can't figure out what I'm doing wrong. Please help!

Upvotes: 0

Views: 69

Answers (1)

aimme
aimme

Reputation: 6773

Change

$table->foreign('sub_category_id')->references('sub_categories')
                    ->on('id')->onDelete('cascade');

to

$table->foreign('sub_category_id')->references('id')
                    ->on('sub_categories')->onDelete('cascade');

and

$table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');

on both schemas like this:

Sub-Categories:

class CreateSubCategoriesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('sub_categories', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();

            $table->string('name', 100);
            $table->integer('category_id')->unsigned();

            $table->foreign('category_id')->references('id')
                    ->on('categories')->onDelete('cascade');
        });
    }

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

Services

class CreateServicesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('services', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();

            $table->string('name', 100);
            $table->integer('sub_category_id')->unsigned();

            $table->foreign('sub_category_id')->references('id')
                    ->on('sub_categories')->onDelete('cascade');
        });
    }

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

Upvotes: 2

Related Questions