Laravel Schema onDelete set null

It is impossible to figure out how to set the correct onDelete restriction for a table in Laravel. (I work with SqLite)

$table->...->onDelete('cascade'); // works $table->...->onDelete('null || set null'); // neither of them work 

I have 3 migrations creating a gallery table:

 Schema::create('galleries', function($table) { $table->increments('id'); $table->string('name')->unique(); $table->text('path')->unique(); $table->text('description')->nullable(); $table->timestamps(); $table->engine = 'InnoDB'; }); 

Creating an image table:

 Schema::create('pictures', function($table) { $table->increments('id'); $table->text('path'); $table->string('title')->nullable(); $table->text('description')->nullable(); $table->integer('gallery_id')->unsigned(); $table->foreign('gallery_id') ->references('id')->on('galleries') ->onDelete('cascade'); $table->timestamps(); $table->engine = 'InnoDB'; }); 

Linking a gallery table with an image:

 Schema::table('galleries', function($table) { // id of a picture that is used as cover for a gallery $table->integer('picture_id')->after('description') ->unsigned()->nullable(); $table->foreign('picture_id') ->references('id')->on('pictures') ->onDelete('cascade || set null || null'); // neither of them works }); 

I do not get any errors. In addition, even the cascade option does not work (only in the gallery table). When you delete the gallery, all pictures are deleted. But when removing the cover, do not delete the gallery (for testing purposes).

Since even a cascade does not start, I set zero is not a problem.

EDIT (workaround):

After reading this article, I changed my outline a bit. Now the image table contains the "is_cover" cell, which indicates whether this image is a cover on his album or not.

The solution to the original problem is still much appreciated!

+59
php laravel laravel-migrations
Jan 01 '14 at 13:23
source share
4 answers

If you want to set null to delete:

 $table->...->onDelete('set null'); 

First, make sure the foreign key field is set to nullable:

 $table->integer('foreign_id')->unsigned()->nullable(); 
+147
Sep 10 '14 at 12:46 on
source share

According to

http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html

$ table-> onDelete ('set null') should work, supposedly try

 $table->...->onDelete(DB::raw('set null')); 

If there are any errors, it would also be helpful

+3
Jan 01 '14 at 13:32
source share
  • This is a known issue in Laravel. Read more about it here .

  • This feature is not supported in SQLite, see here

  • Also a topic that contains a detailed description of this problem.

+2
Jan 02 '14 at 0:40
source share

Using Laravel 4.2 in MySQL 5.5 using InnoDB works onDelete ('set null').

+2
Aug 26 '14 at 10:25
source share



All Articles