3 ms·
Uh, no. In your scenario, Users cannot exist without being a member of one and only one Group. We'll accept that as a business requirement. It's a peculiar req
by taylodl 2y ago
Uh, no.
In your scenario, Users cannot exist without being a member of one and only one Group. We'll accept that as a business requirement. It's a peculiar requirement, but fine, we can work with that.
The application user attempts to delete a particular Group that has one or more User records with foreign keys pointing back to it. Furthermore, the User entity has a constraint that the Group foreign key cannot be NULL - what should happen?
The API that you're using to access your database - you're not directly accessing your database, are you? - should detect that the delete failed, query that Users exist and return that condition. The application then knows that Users exist in the Group attempting to be deleted.
You have 2 choices at this point, and one of those choices has an option:
1. Inform the application user of the situation and inform them they must delete all the Users prior to deleting the Group
2. Inform the application user of the situation and inform them they must delete all the Users prior to deleting the Group, or they can force delete the Users
Option 1 - If the application user requests to force delete the Users then delete them and then delete the Group
Option 2 - If the application user requests to force delete the Users then move those Users to an Ungrouped Users table (so-called soft delete) and then delete the Group.
That's it. Those are your options. WRT to auditing, all I expect to be audited is the Group delete request: who made the request, when was the request made, what Group was requested to be deleted, was a force delete requested, and what was the result.
If you need to enable undo functionality, then there are other ways of achieving that which are completely separate from audit logging as undo is a separate concern from auditing. Keep the concerns separate in your implementation.
In short, soft deletes aren't "evil", and in fact, may be a business requirement.