Patient code unique within an organisation: Organization on Patient and a database constraint

Open
#2,628 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
django, postgresql, python
Domain
backend, database

Research direction

Start by reading the Patient model and its visibility logic, then trace Patient.match and the patient form, sample/cohort dialog, API serializer, and CSV import. First run the provided duplicate-code query on deployments; the issue leaves unresolved how to assign an organization when a patient is shared across organizations. Done means organization-scoped matching and uniqueness work across every create path, with existing data handled.

Written by the indexing model from the issue text.

Description

🤖 Written by Claude

Follows #2518, where the form, create-patient dialog and API refuse a patient code another visible patient already has. That is a visibility-scoped check in Python, not a database constraint, so a race, a superuser or a direct write can still create a duplicate.

Problem

Patient has no lab or organisation column: visibility is Guardian permissions on users and groups (labs and organisations are groups), so there is nothing to scope a UniqueConstraint on. Patient.match and the CSV import already treat a code as the sole identity and raise "Matched multiple patients" on a duplicate.

A global unique index on patient_code is not right: on a shared server, unrelated labs' short de-identified codes ("P001") would collide. Within one organisation (SA Path) a patient legitimately moves between labs, so the organisation is the scope.

Proposed fix

  • Patient.organization FK, with a partial UniqueConstraint on (organization, patient_code) where the code is non-blank.
  • Backfill from the group object permissions (lab group -> lab -> organisation); decide what to do when a patient is shared with several organisations' groups.
  • Every create path sets it: the patient form, the sample/cohort create-patient dialog, the API serializer and the CSV import. A user in several organisations needs a chooser; a user in one gets it set.
  • Patient.match scopes by organisation as well as visibility.

Before building it: count duplicate codes on each deployment to see whether any exist.

SELECT patient_code, count(*) FROM patients_patient
WHERE coalesce(btrim(patient_code), '') <> ''
GROUP BY patient_code HAVING count(*) > 1;
Dominant language
Python
Stars
31
Forks
4
Avg merge
4h 52m
Merged PRs (30d)
183

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from SACGF/variantgrid

All issues in SACGF/variantgrid

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.