I fixed it.
This is what I did
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddMenuSeoForeignKey extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//
Schema::table('menu', function($table){
//$table->unsignedInteger('seo_id');
$table->unsignedInteger('seo_id')->nullable();
$table->foreign('seo_id')->references('id')->on('seo')->onDelete('cascade')->onUpdate('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
Thanks! I was having the same problem... why all the examples don't use nullable and still work, I don't know.
narfed said:
Thanks! I was having the same problem... why all the examples don't use nullable and still work, I don't know.
Totally agree. This post saved me. Thanks :)
It works without nullable if you haven't already inserted data into the table with the FK column. If you already have 1+ records, you cannot add the FK column as records exist without it being set (at least that was the case for me).
Spent about half and hour. Running out of time! Thanks for the solution hehe.
if someone still have this problem when Updating the table to add a foreign key, and this table has content in it , remember to change the 'seo_id' (for example in your menu table) for all the content to an existed id in seo table.
I didn't want the field to be nullable, but I wanted to be able to add the row even if there was data in my table. The best way to get around that is by disabling foreign key checks. Here's my code:
DB::statement('SET FOREIGN_KEY_CHECKS=0;');
Schema::table('foo_products', function(Blueprint $table)
{
$table->unsignedInteger('foo_type_id');
$table->foreign('foo_type_id')->references('id')->on('foo_types');
});
DB::statement('SET FOREIGN_KEY_CHECKS=1;');
alanmanderson said:
I didn't want the field to be nullable, but I wanted to be able to add the row even if there was data in my table. The best way to get around that is by disabling foreign key checks.
Nice trick! I successfully use this.
alanmanderson said:
I didn't want the field to be nullable, but I wanted to be able to add the row even if there was data in my table. The best way to get around that is by disabling foreign key checks. Here's my code:
DB::statement('SET FOREIGN_KEY_CHECKS=0;'); Schema::table('foo_products', function(Blueprint $table) { $table->unsignedInteger('foo_type_id'); $table->foreign('foo_type_id')->references('id')->on('foo_types'); }); DB::statement('SET FOREIGN_KEY_CHECKS=1;');
I also used this successfully!!
Having some relations to null might be problematic for the application, so another option to consider is adding a default argument with the ID for a known row in the foreign table:
$table->integer('seo_id')->unsigned()->default(1);
$table->foreign('seo_id')
->references('id')->on('seo');
alanmanderson said:
I didn't want the field to be nullable, but I wanted to be able to add the row even if there was data in my table. The best way to get around that is by disabling foreign key checks. Here's my code:
DB::statement('SET FOREIGN_KEY_CHECKS=0;'); Schema::table('foo_products', function(Blueprint $table) { $table->unsignedInteger('foo_type_id'); $table->foreign('foo_type_id')->references('id')->on('foo_types'); }); DB::statement('SET FOREIGN_KEY_CHECKS=1;');
This is great solution!! Thanks a lot sir! :)
Thank you! It solved my problem of having a foreign table in a column inserted by a Schema::table
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community