douse8732 2013-08-12 15:31
浏览 27

在主表中软删除时如何关闭数据删除?

I have a Users and a Groups table in Laravel's Eloquent ORM. There can be multiple users in a group and a user can be in multiple groups, so I use a pivot table to realize the many-to-many relationship. In real, the relationship isn't many-to-many because every user can be in only one group, but the system was designed this way. I soft delete the rows in the Users table so if needed I can restore users later.

The problem is that when I delete a user, the system automatically delete the entry in the pivot table too. This is always one entry. I didn't set it to behave like this, only added the protected $softDelete = true; line to the Users model, so I don't understand why the system automatically delete the pivot entries.

I don't want to soft delete the pivot entries, I want to soft delete only and exclusively the users, and the system shouldn't touch anything else.
I could create my own delete function that set the deleted_at variable to the actual time but this way I couldn't simply turn of the soft delete by changing the true to false if I need this.

Why does the system automatically delete the pivot entries, and how can I turn off this behavior?

  • 写回答

1条回答 默认 最新

  • douzhaocheng4533 2013-09-17 12:46
    关注

    Check your database if you are using foreign keys you may have it set to ON UPDATE, ON DELETE "CASCADE". You need to set it up to "NO ACTION"

    CONSTRAINT `fk_USER_Address_1` FOREIGN KEY (`userId`) REFERENCES `USER` (`userId`) ON UPDATE CASCADE ON DELETE CASCADE,
    

    to

    CONSTRAINT `fk_USER_Address_1` FOREIGN KEY (`userId`) REFERENCES `USER` (`userId`) ON UPDATE CASCADE ON DELETE NO ACTION,
    
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助