Support the ongoing development of Laravel.io →
posted 10 years ago
Eloquent

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

Last updated 3 years ago.
0

After talking It seems to be a bug so I filed in a bug report : https://github.com/laravel/framework/issues/6655

Roelof

0

I believe that you should make the category_id filed unsinged as per documentation

$table->integer('category_id')->unsigned();
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.