无法理解如何在Laravel中对表设置正确的onDelete约束.(我正在与SqLite合作)
$table->...->onDelete('cascade'); // works
$table->...->onDelete('null || set null'); // neither of them work
我有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';
});
正在创建图片表格:
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';
});
将图库表链接到图片:
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
});
我没有收到任何错误.此外,即使是"层叠"选项也不起作用(仅在 gallery 桌子上).删除图片库将删除所有图片.但是删除封面图片并不会删除图库(出于测试目的).
因为连"级联"都没有触发,所以我"设为空"不是问题.
EDIT (workaround):
在读完这article篇文章后,我稍微改变了一下我的模式.现在,Pictures表包含一个"is_cover"单元格,表示此图片是否为其相册的封面.
我们仍然高度赞赏解决原来问题的方法!