Skip to content

[16.0][FIX] ni_observation: pick the latest observation deterministically - #163

Merged
piruin merged 1 commit into
nirun-life:16.0from
wasud:16.0-fix-observation-latest
Oct 18, 2026
Merged

piruin merged 1 commit into
nirun-life:16.0from
wasud:16.0-fix-observation-latest

Conversation

@wasud

@wasud wasud commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

What changed

  • ni.patient.observation and ni.encounter.observation (latest observation per patient/encounter and type): ORDER BY … occurrence DESC, id DESC.
  • New tests/test_latest_observation.py.
  • ni_observation 16.0.0.6.0 → 16.0.0.6.1, so -u recreates the views.

Why

The views use DISTINCT ON (…, type_id) ordered by occurrence only. When two observations of a type share an occurrence (for example two questionnaires mapped to the same type, completed in one transaction), PostgreSQL may return either row. The patient and encounter forms then read a row that the next query no longer returns, and fail with "records … cannot be found".

Found by the Phase 1 data migration pilot (#141, findings comment). The same fix is in the 17.0/18.0/19.0 migration PRs (#156, #157, #158); this is the 16.0 backport for production. Part of P0-4 (#135), epic #131.

How tested

  • --test-tags /ni_observation on a fresh 16.0 DB: 27 tests, 0 failed.
  • Without the view change, test_same_occurrence_picks_the_last_record fails.
  • pre-commit clean.

Notes for reviewer

  • No data change: the views are recreated on -u ni_observation.
  • Production may already have such ties (questionnaires that write the same observation type). After the update, the forms show the observation with the highest id for that time.

The latest-observation views keep one row per patient (or encounter) and type
with DISTINCT ON, ordered by occurrence only. When two observations of a type
share an occurrence (created in the same transaction, for example by two
questionnaires mapped to the same type), PostgreSQL may return either row. The
patient and encounter forms then read a row that the next query no longer
returns, and fail with "records ... cannot be found". Break the tie by id.

Found by the Phase 1 data migration pilot (nirun-life#141); 16.0 has the same views.
@piruin
piruin merged commit 9b35b1f into nirun-life:16.0 Oct 18, 2026
4 checks passed
@piruin piruin mentioned this pull request Oct 19, 2026
4 tasks done
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.

2 participants