Hello,
I have this migration file
<?php
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->foreign('category_id')->references('id')->on('categories');
$table->string('title');
$table->text('description');
$table->decimal('price', 6, 2);
$table->boolean('availability')->default(1);
$table->string('image');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('products');
}
}
but when I do php artisan migrate I see this output :
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `products` add constraint products_category_id_foreign foreign key (`catego
ry_id`) references `categories` (`id`))
Roelof
After talking It seems to be a bug so I filed in a bug report : https://github.com/laravel/framework/issues/6655
Roelof
I believe that you should make the category_id filed unsinged as per documentation
$table->integer('category_id')->unsigned();
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community