I have an active record based ORM with soft deletes (Laravel Eloquent to be specific) in an SQL database.
The 'soft delete' behavior that is built in to the ORM is:
If the record is not deleted, then deleted_at
is null. When a record is soft deleted, the deleted_at
field is populated with a timestamp.
The ORM uses the 'deleted_at' field internally in its logic as well as creates the field. It is difficult to change the type of deleted_at
.
I have the situation where I have three models (tables), A, B, and C. Model C belongs to both A and B. Neither A or B belong to each other. So table C contains columns to hold foreign keys to A and B.
For C to be valid, both of its parents must not be deleted. If either A or B is deleted, then C must also be deleted.
However, if A or B is deleted, then restored (i.e. deleted_at
column set back to null), then C should also be restored.
I'm trying to figure out an algorithm to determine when to restore C, if A and B are restored.
I'm leaning toward a reference counting algorithm? So when either A or B is deleted, it deletes C and increments C's deleted_count
. If A or B is restored, then decrement C deleted_count
and if deleted_count
is 0, then restore C?
Does this sound like the best approach for this type of situation? Any obvious drawbacks?