How to disable arch deletion during soft deletion in the main table?

I have a Users and Groups table in Laravel Rloquent ORM. A group can have multiple users, and a user can be in multiple groups, so I use a pivot table to implement many-to-many relationships. In reality, the relationship is not many-to-many, because each user can only be in one group, but the system was designed in this way. I gently delete the rows in the Users table, so if necessary, I can restore users later.

The problem is that when a user is deleted, the system automatically deletes the entry in the pivot table. This is always one entry. I did not behave like this, added the line protected $softDelete = true; to the Users model, so I don’t understand why the system automatically deletes summary data entries.

I do not want to softly delete the summary data records, I want only soft deletion and exclusively for users, and the system should not touch anything else.
I could create my own delete function that sets the deleted_at variable at the actual time, but that way I could not just enable soft delete by changing true to false if I need it.

Why does the system automatically delete summary records and how can I disable this behavior?

+4
source share
1 answer

Check your database if you use foreign keys that you can set to ON UPDATE, ON DELETE "CASCADE". You need to set it 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, 
+1
source

Source: https://habr.com/ru/post/1496623/


All Articles