Skip to content

fix: a group that cannot be deleted says why - #888

Merged
blaipr merged 1 commit into
mainfrom
fix/a-group-that-cannot-be-deleted-says-why
Aug 26, 2026
Merged

fix: a group that cannot be deleted says why#888
blaipr merged 1 commit into
mainfrom
fix/a-group-that-cannot-be-deleted-says-why

Conversation

@blaipr

@blaipr blaipr commented Aug 26, 2026

Copy link
Copy Markdown
Member

Deleting a user group that something still holds came back as "The record is in use" —
MySQL error 1451 translated by the database layer — which names nothing. The group's own
"Used by" panel was no help either: it lists the group's users, so a group with no
members but a hundred accounts scoped to it looked entirely safe to delete right up until
it was not.

The delete now asks first, and the refusal says what is holding it: "Group in use", with
"Accounts: 2 - Accounts in history: 1".

Getting that right meant reading the schema rather than the existing helper. Six foreign
keys point at UserGroup and only three of them refuse a delete:

fk_User_userGroupId            RESTRICT  a user whose main group this is
fk_Account_userGroupId         RESTRICT  an account owned by the group
fk_AccountHistory_userGroupId  RESTRICT  a history row, which outlives its account

fk_AccountToUserGroup_userGroupId  CASCADE
fk_ItemPreset_userGroupId          CASCADE
fk_UserToUserGroup_userGroupId     CASCADE

getUsage() had it wrong in both directions: it queried UserToUserGroup and
AccountToUserGroup, which cascade, and not AccountHistory, which does not. So wiring it up
as it stood would have refused deletes the database would have allowed — a group whose
only "usage" is the members it is supposed to have — while still letting through the one
case that actually fails. It asks for exactly the three RESTRICT relations now.

That method had never run. It has no caller anywhere in src, and the coverage report shows
count="0" on both it and the service method in front of it — which is presumably how it
came to disagree with the schema without anyone noticing.

The foreign keys still stand behind all this: a row created between the check and the
delete is refused by the database exactly as before. This is about telling an
administrator what to go and fix.

This is the same defect the codebase already fixed for individual users —
User::getUsageForUser() carries a comment about the panel disagreeing with the delete —
and it was never carried across to groups.

Checked by making the refusal unreachable: the new service test fails.

Deleting a user group that something still holds came back as "The record is in use" —
MySQL error 1451 translated by the database layer — which names nothing. The group's own
"Used by" panel was no help either: it lists the group's *users*, so a group with no
members but a hundred accounts scoped to it looked entirely safe to delete right up until
it was not.

The delete now asks first, and the refusal says what is holding it: "Group in use", with
"Accounts: 2 - Accounts in history: 1".

Getting that right meant reading the schema rather than the existing helper. Six foreign
keys point at UserGroup and only three of them refuse a delete:

    fk_User_userGroupId            RESTRICT  a user whose main group this is
    fk_Account_userGroupId         RESTRICT  an account owned by the group
    fk_AccountHistory_userGroupId  RESTRICT  a history row, which outlives its account

    fk_AccountToUserGroup_userGroupId  CASCADE
    fk_ItemPreset_userGroupId          CASCADE
    fk_UserToUserGroup_userGroupId     CASCADE

`getUsage()` had it wrong in both directions: it queried UserToUserGroup and
AccountToUserGroup, which cascade, and not AccountHistory, which does not. So wiring it up
as it stood would have refused deletes the database would have allowed — a group whose
only "usage" is the members it is supposed to have — while still letting through the one
case that actually fails. It asks for exactly the three RESTRICT relations now.

That method had never run. It has no caller anywhere in src, and the coverage report shows
count="0" on both it and the service method in front of it — which is presumably how it
came to disagree with the schema without anyone noticing.

The foreign keys still stand behind all this: a row created between the check and the
delete is refused by the database exactly as before. This is about telling an
administrator what to go and fix.

This is the same defect the codebase already fixed for individual users —
`User::getUsageForUser()` carries a comment about the panel disagreeing with the delete —
and it was never carried across to groups.

Checked by making the refusal unreachable: the new service test fails.
@blaipr
blaipr merged commit a243490 into main Aug 26, 2026
8 checks passed
@blaipr
blaipr deleted the fix/a-group-that-cannot-be-deleted-says-why branch August 26, 2026 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant