Problem with foreign key with the Laravel Schema Builder

I was trying to set a foreign key to a field but I found myself getting this error.

[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table 'database.#sql-448_10c' (errno: 150) (SQL: alter table `table` add constraint articletypes_articletype_id_foreign foreign key (`articletype_id`) references `articletypes` (`id`))

After a little time searching for this I found a solution. The problem it’s actually a simple problem. By default the Schema Builder sets all “increments” fields to Unsigned so the field in cuestion should be unsigned.

$table->increments('id');
$table->string('article_type', 120);
$table->integer('articletype_id')->nullable()->unsigned();
$table->foreign('articletype_id')->references('id')->on('articletypes');

Stackoverflow Answer

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s