php Laravel 架构 onDelete 设置为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20869072/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Schema onDelete set null
提问by M K
Can't figure out how to set proper onDelete constraint on a table in Laravel. (I'm working with SqLite)
无法弄清楚如何在 Laravel 中的表上设置正确的 onDelete 约束。(我正在使用 Sqlite)
$table->...->onDelete('cascade'); // works
$table->...->onDelete('null || set null'); // neither of them work
I have 3 migrations, creating the gallery table:
我有 3 次迁移,创建了图库表:
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 the pictures 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 gallery table to a picture:
将图库表链接到图片:
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 receive any errors. Also, even the "cascade" option doesn't work (only on the gallery table). Deleting a gallery deletes all pictures. But deleting the cover picture, wont delete the gallery (for test purposes).
我没有收到任何错误。此外,即使“级联”选项也不起作用(仅在图库表上)。删除图库会删除所有图片。但是删除封面图片,不会删除图库(用于测试目的)。
Since even the "cascade" is not triggered, I "set null" is not the problem.
由于甚至没有触发“级联”,因此我“设置为空”不是问题。
EDIT (workaround):
编辑(解决方法):
After reading this articleI've changed my schema a bit. Now, the pictures table contains an "is_cover" cell, that indicates whether this picture is a cover on its album or not.
读完这篇文章后,我稍微改变了我的架构。现在,图片表包含一个“is_cover”单元格,指示这张图片是否是其相册的封面。
A solution to the original problem is still highly appreciated!
仍然高度赞赏原始问题的解决方案!
回答by Johan
If you want to set null on delete:
如果要在删除时设置 null:
$table->...->onDelete('set null');
First make sure you set the foreign key field as nullable:
首先确保将外键字段设置为可为空:
$table->integer('foreign_id')->unsigned()->nullable();
回答by M K
回答by Chris Barrett
According to
根据
http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html
http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html
$table->onDelete('set null') should work prehaps try
$table->onDelete('set null') 应该工作 prehaps try
$table->...->onDelete(DB::raw('set null'));
If there are any errors, would also be helpful
如果有任何错误,也会有所帮助
回答by Mark Kendall
Using Laravel 4.2 on MySQL 5.5 with InnoDB, onDelete('set null') works.
在带有 InnoDB 的 MySQL 5.5 上使用 Laravel 4.2,onDelete('set null') 有效。

