php setnull,php – Laravel Schema onDelete set null

无法弄清楚如何在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”单元格,表示这张图片是否是它的相册上的封面。

对原始问题的解决方案仍然高度赞赏!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值