Laravel General error: 1215 Cannot add foreign key constrain
Posted: Wed Sep 23, 2015 4:05 am
I just created a migration file ,
now when i run the command in my CMD ,
I get the following error :
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 ?
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');
}
}
Code: Select all
php artisan migrateCode: 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 migratei 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 ?