Laravel General error: 1215 Cannot add foreign key constrain

Discussion for various published PHP frameworks, including Zend Framework, CodeIgniter, Kohana, CakePHP, Yii, Symfony, and others.

Moderator: General Moderators

Post Reply
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

Laravel General error: 1215 Cannot add foreign key constrain

Post 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 ?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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?
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

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

Post 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');
	}

}

User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

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

Post by gautamz07 »

The tables have no values in them , they are empty !
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

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

Post 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)
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

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

Post by gautamz07 »

you are right ! one int(10) and the other is int(11) , how do i change that ? :P
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

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

Post by gautamz07 »

$table->integer('post')->length(10);

is the above the correct syntax ?
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

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

Post 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 ! :(
User avatar
gautamz07
Forum Contributor
Posts: 331
Joined: Wed May 14, 2014 12:18 pm

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

Post 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
Post Reply