Support the ongoing development of Laravel.io →
posted 9 years ago
Database
Last updated 1 year ago.
0
Solution

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()
	{
		//
	}

}
Last updated 1 year ago.
0

Thanks! I was having the same problem... why all the examples don't use nullable and still work, I don't know.

Last updated 1 year ago.
0

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 :)

0

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).

0

Spent about half and hour. Running out of time! Thanks for the solution hehe.

0

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.

0

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;');
0

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.

Last updated 8 years ago.
0

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!!

0

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');
0

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! :)

Last updated 6 years ago.
0

Thank you! It solved my problem of having a foreign table in a column inserted by a Schema::table

0

Sign in to participate in this thread!

Eventy

Your banner here too?

shiva shiva Joined 24 Jul 2014

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.

© 2024 Laravel.io - All rights reserved.