Bugfix/fix orphaned certificate pivot rows#522
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #522 +/- ##
============================================
+ Coverage 40.77% 40.79% +0.02%
- Complexity 630 632 +2
============================================
Files 99 100 +1
Lines 2362 2368 +6
============================================
+ Hits 963 966 +3
- Misses 1399 1402 +3 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| static::deleted(function($certificate){ | ||
| $certificate->users()->detach(); | ||
| }); | ||
| } |
There was a problem hiding this comment.
Note that this defeats most of the purpose of using soft deletes for this model. After all, the certificate itself consists merely of a name and abbreviation. The most valuable certificate data lies in its relations, i.e., which users the certificate is/was assigned to.
In Laravel, soft deletes behave the same as actual deletes, unless the deleted records are explicitly requested using ->withTrashed(). Note that this allows us to keep the "pivot rows" of removed certificates while automatically hiding them when not requested. This makes restoring accidental deletes possible.
Observe that the certificate BelongsTo relation on the User model explicitly requests the certificates withTrashed:
Lines 94 to 95 in f68854c
In case we want to exclude removed certificates from the user's certificate overview (which IMO we do), we should simply remove the ->withTrashed() from that line. That would remove the need to detach on delete as well as the migration.
Add migration to cleanup old "ghost" pivot entries. Then add logic to detach users from certificates when the certificate gets deleted.