Table of Contents

#199 — A duplicate item name hits an unhandled uniqueness constraint and returns a 500 error page instead of a validation message (Resolved)

← Back to bug list · View issue #199 in MantisBT


Reported by: Nour aboulhosn on 2026-08-08T12:19:08+01:00
Assigned to: Claude
Resolved by: Claude on 2026-08-08T14:07:34+01:00
Severity: major
Priority: high
Reproducibility: have not tried
Category: General

Description: Saving a new item called `QA Unique 8081` succeeds with HTTP 200. Saving a second item with that same name returns HTTP 500 Internal Server Error — not a message, an error page. It happens regardless of the price or unit entered, and the check is case-insensitive: `qa unique 8081` also 500s.

The constraint is right; the handling is not. A user who reuses a name they already have is thrown out of the app with nothing explaining what went wrong, and reusing a name is an ordinary thing to do when correcting an item that already exists.

Catching the duplicate before insert and returning “You already have an item called that.” would fix it.

Steps to Reproduce: 1. Go to Saved items. Add an item: name `QA Unique 8081`, price `10.00`, per `ea`. It saves — HTTP 200. 2. Add another item with the same name, `QA Unique 8081`, price `10.00`, per `ea`. 3. The request returns HTTP 500 Internal Server Error. 4. Repeat with the same name but a different price and unit (`99.00`, `kg`) — still 500. 5. Repeat with `qa unique 8081` in lower case — still 500, so the uniqueness check ignores case. 6. Any genuinely new name, such as `QA Unique 8082`, saves normally at HTTP 200, which isolates the duplicate name as the cause.

Resolution: What — saving a second item with a name already on the list returned HTTP 500 and the generic error page, case-insensitively.

Where — `ItemController::validated()` / `App\Billing\CatalogueItem`.

Why — `uq_catalogue_items_name (company_id, active_name_key)` is right and stays; nothing caught the 1062 it raises. Reusing a name is an ordinary thing to do when correcting an item you already have, so the commonest mistake on that screen threw the reader out of the app.

Fix — `CatalogueItem::nameExists()` asks before the insert, using the column's own comparison (`=` under `utf8mb4_unicode_ci`) rather than a PHP `strcasecmp`, which would agree on ASCII and disagree on “Café”/“CAFE” — and the disagreement would land as the 500 this exists to prevent. Asked only once the shape checks have passed, so an empty or overlong name still gets its own specific sentence. `store()` resolves the row being edited BEFORE validating, so an edit is excluded from its own check and a posted id belonging to another company (which `find()` cannot see) becomes an ordinary create.

Verified on the running app: the first save succeeds; a second with the same name, and a third with `qa unique 8081`, both return 302 with “You already have an item called that.” rendered on the form, and one row exists. Editing that item with its own name unchanged still saves (price 10 → 14.50).

Activity / Notes

Where — `ItemController::validated()` / `App\Billing\CatalogueItem`.

Why — `uq_catalogue_items_name (company_id, active_name_key)` is right and stays; nothing caught the 1062 it raises. Reusing a name is an ordinary thing to do when correcting an item you already have, so the commonest mistake on that screen threw the reader out of the app.

Fix — `CatalogueItem::nameExists()` asks before the insert, using the column's own comparison (`=` under `utf8mb4_unicode_ci`) rather than a PHP `strcasecmp`, which would agree on ASCII and disagree on “Café”/“CAFE” — and the disagreement would land as the 500 this exists to prevent. Asked only once the shape checks have passed, so an empty or overlong name still gets its own specific sentence. `store()` resolves the row being edited BEFORE validating, so an edit is excluded from its own check and a posted id belonging to another company (which `find()` cannot see) becomes an ordinary create.

Verified on the running app: the first save succeeds; a second with the same name, and a third with `qa unique 8081`, both return 302 with “You already have an item called that.” rendered on the form, and one row exists. Editing that item with its own name unchanged still saves (price 10 → 14.50).

Full History

Date User Field Change
2026-08-08T12:19:08+01:00 Nour aboulhosn issue-new New Issue
2026-08-08T14:07:29+01:00 Claude note-added Note Added: 0000157
2026-08-08T14:07:33+01:00 Claude tag-added Tag Attached: done-by-claude
2026-08-08T14:07:34+01:00 Claude Assigned To ⇒ Claudine
2026-08-08T14:07:34+01:00 Claude Status new ⇒ resolved

Synced automatically from MantisBT issue #199.