2501 — building change on a room row must yield an incomplete row, not a 422¶
Root cause¶
Inline table edits send single-field PATCHes
(ModuleTable.vue saveEdit → patchItem({[col.field]: value})). For the
buildings room handler, building_name is the kind_field and room_type
the subkind_field (backend/app/modules/buildings/handlers.py), so a
{building_name} PATCH triggers clear_dependent_fields_on_kind_change
(backend/app/services/module_handler_service.py), which nulls room_type.
The emission recompute then hits resolve_building_rooms
(backend/app/modules/buildings/emissions.py), where the empty room_type
raises EmissionTypeResolutionError; the update workflow maps the
ValueError to HTTP 422 and rolls back
(backend/app/workflows/carbon_report_module.py). Net: nobody can change
the building on an existing room row. No data is corrupted (full rollback).
This is not a #2050 regression in substance: before the fail-hard (shipped
to stage 2026-08-27), the same flow silently nulled room_type and zeroed
the row's emissions. The guardrail made an old silent bug loud.
Reproduced (stage, report 456): PATCH …/buildings/building/<id> with
{"building_name": "BCH"} → 422 No emission type for building room_type ''.
Decided approach (maintainer, issue comment 2026-08-28)¶
Authorize an incomplete row. When building_name changes, the stored
room_name cannot survive (a room belongs to exactly one building), so the
backend clears room_name and room_type; the row persists without an
emission — same semantic as an incomplete equipment row (no factor → no
emission rows → blank kg cell). The alternative (disable building edits)
was considered and rejected.
Key precedents this mirrors:
- Equipment:
resolve_computationsreturns[]when no factor resolves;prepare_createtreats an empty leaf list as "a real answer", andupsert_by_data_entrydeletes stale emission rows even when the new set is empty — the row cleanly loses its kg_co2eq. - The room dialog (
useBuildingRoomDynamicOptions.ts) already clears room fields client-side on building change. - The frontend already has generic incomplete-row rendering:
submoduleConfig.requiredFieldIds→row-incompleteclass (ModuleTable.vueisComplete).
Backend changes¶
-
Clear
room_nameon kind change. Extendclear_dependent_fields_on_kind_changewith a handler-declaredkind_dependent_fields: tuple[str, ...] = ()onBaseModuleHandler, cleared under the same conditions as the subkind (kind changed, field not in the request).BuildingRoomModuleHandlerdeclares("room_name",).room_typeis already cleared as the subkind.BuildingRoomHandlerUpdateacceptsroom_name=Noneas-is (its_non_emptyvalidator skipsNone). -
Incomplete gate in emission resolution. In
resolve_building_rooms, before the room_type validity check: ifroom_nameorroom_typeis missing/empty, return[](no leaves → no emission rows → stale rows deleted by the upsert). The existing raises stay for present but invalid values: an unknownroom_typestill raises, and aroom_namethat resolves noBuildingRoomref-data row still fails hard in the formula (bad data ≠ incomplete). This also un-breaks recalc over legacy rows corrupted by the pre-#2050 silent clear (room_type null, stale room_name): they become visible incomplete rows instead of per-entry recalc failures. -
Embodied-energy companion: no change.
EmbodiedEnergyWorkflow. _reconcileis already a pure function of the parents'room_names: a parent losing its room_name drops its companion, re-picking a room recreates it. Pinned by test, not changed.
Frontend changes¶
-
Incomplete-row rendering. Add
requiredFieldIds: ['building_name', 'room_name', 'room_type']to the Building submodule config (frontend/src/constant/module-config/ buildings.ts) so the genericrow-incompletestyling and completeness logic apply. The kg cell is blank because the backend returnskg_co2eq: null. -
Inline room pick sends the room's type. When the user picks a
room_nameinline, PATCH{room_name, room_type}with the type from the picked room's ref-data (the building-rooms endpoint returnsroom_typeper room) — mirroring the dialog's autofill. Without this, a room re-pick after a building change leaves the row incomplete until the user separately setsroom_type, and a plain room change silently keeps the old room's type (a lab priced as an office). Payload/option mapping extracted tofrontend/src/utils/buildingRoomInline.ts. -
Inline
room_nameoptions source (investigated: wrong source).ModuleInlineSelectfed the Local column from the factor taxonomy's subkind options — which for buildings are room types ("auditoriums", …), not room names. Picking one PATCHedroom_name: "auditoriums"→ unknown room → the deep formula 422 (the issue's second repro). The Local select now loads the ref-data rooms of the row's current building viabuildingRoomStore.fetchRooms(store-cached per building, 60 s), the same source as the dialog, and refreshes when the row's building changes.
Tests (regression-first)¶
- Backend (pytest, pg): PATCH
{building_name}on a complete room row → 200,room_name/room_typenull in response, emission rows deleted,kg_co2eqnull. Then PATCH{room_name, room_type}→ row complete, emissions recomputed. Companion embodied row deleted on the first PATCH, recreated on the second. - Backend unit: the clear function nulls declared
kind_dependent_fieldson kind change only (same-value PATCH untouched; explicit values in the request win). - Backend:
resolve_building_roomsreturns[]for missing room_name/room_type, still raises for an invalid room_type. - Frontend (Playwright): inline building change renders the row as
incomplete with an empty kg cell; picking a room of the new building
completes it. Component test for the packed
{room_name, room_type}PATCH payload.
Out of scope / follow-ups¶
- Write-time validation that
(building_name, room_name)matchesBuildingRoomref-data (would turn the deep formula failure for unknown rooms into a clean 422 naming the field). Park as its own issue. - Inline-select revert-on-error (the select keeps the failed value after
a 422,
error: false). Mostly moot once this flow stops 422ing. - CSV/import paths: unchanged —
pre_computewarnings for unresolvable rooms remain.