无法弄清楚如何在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
});
我没有收到任何错误。此外,即使“级联”选项不工作(只在图库表上)。删除图库将删除所有照片。但是删除封面图片,不会删除图库(用于测试目的)。
因为即使“级联”没有被触发,我“设置null”不是问题。
EDIT(解决方法):
阅读这个article后,我改变了我的架构一点。现在,图片表包含一个“is_cover”单元格,表示这张图片是否是它的相册上的封面。
对原始问题的解决方案仍然高度赞赏!