Page 1 of 1

Laravel General error: 1215 Cannot add foreign key constrain

Posted: Wed Sep 23, 2015 4:05 am
by gautamz07
I just created a migration file ,

Code: Select all

<?php

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

class CreateProductsTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('products' , function($table){
            $table->increments('id');
            $table->integer('category_id');
            $table->string('title');
            $table->text('description');
            $table->decimal('height' , 6 , 2);
            $table->decimal('width' , 6 , 2);
            $table->decimal('length' , 6 , 2);
            $table->string('color');
            $table->string('material');
            $table->timestamps();

        });

        Schema::table('products' , function($table){
            $table->foreign('category_id')->references('id')->on('categories');
        });

    }

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

}
now when i run the command in my CMD ,

Code: Select all

php artisan migrate
I get the following error :

Code: Select all

[Illuminate\Database\QueryException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL : alter table `products` add constraint products_category_id_foreign foreig n key (`category_id`) references `categories` (`id`)) [PDOException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint migrate
So i get the above error , on checking SO for similar issues i got the following http://stackoverflow.com/questions/2261 ... 0#22616100

i spit the creation of the migration file, yet i am getting the same error , WHY ? what would be the other reasons causing this error ? can anybody elaborate and explain ?

Re: Laravel General error: 1215 Cannot add foreign key const

Posted: Wed Sep 23, 2015 6:16 am
by Celauran
Does categories exist? Are categories.id and products.category_id of the same type? Are the tables populated? Do you have conflicting values?

Re: Laravel General error: 1215 Cannot add foreign key const

Posted: Wed Sep 23, 2015 7:30 am
by gautamz07
before i ran the above migration , i ran a migrations for categories table , the migration file looks like below :

Code: Select all

<?php

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

class CreateCategories extends Migration {

	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
        Schema::create('categories' , function($table){
            $table->increments('id');
            $table->string('name');
            $table->timestamps();
        });
	}

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

}


Re: Laravel General error: 1215 Cannot add foreign key const

Posted: Wed Sep 23, 2015 7:30 am
by gautamz07
The tables have no values in them , they are empty !

Re: Laravel General error: 1215 Cannot add foreign key const

Posted: Wed Sep 23, 2015 8:05 am
by Celauran
Are the related column types exactly the same? Does increments() and integer() have the same default? Check for, say, INT(10) v. INT(11)

Re: Laravel General error: 1215 Cannot add foreign key const

Posted: Wed Sep 23, 2015 12:10 pm
by gautamz07
you are right ! one int(10) and the other is int(11) , how do i change that ? :P

Re: Laravel General error: 1215 Cannot add foreign key const

Posted: Wed Sep 23, 2015 12:16 pm
by gautamz07
$table->integer('post')->length(10);

is the above the correct syntax ?

Re: Laravel General error: 1215 Cannot add foreign key const

Posted: Thu Sep 24, 2015 2:12 am
by gautamz07
My migration file looks like so now :

Code: Select all

<?php

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

class CreateProductsTable extends Migration {

    /**
	 * Run the migrations.
	 *
	 * @return void
	 */
    public function up()
    {
        Schema::create('products' , function($table){
            $table->increments('id');
            $table->integer('category_id')->length(10);
            $table->string('title');
            $table->text('description');
            $table->decimal('height' , 6 , 2);
            $table->decimal('width' , 6 , 2);
            $table->decimal('length' , 6 , 2);
            $table->string('color');
            $table->string('material');
            $table->timestamps();

        });

        Schema::table('products' , function($table){
            $table->foreign('category_id')->references('id')->on('categories');
        });

    }

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

}
I have changed :

Code: Select all

  $table->integer('category_id')->length(10);
I still get the same error though ! :(

Re: Laravel General error: 1215 Cannot add foreign key const

Posted: Thu Sep 24, 2015 3:37 am
by gautamz07
Solved it doing:

Code: Select all

      $table->integer('category_id')->unsigned(10);
unsigned(10); makes its int(10) , instead of int(11) , magically , i don't know how :P