rcpch / national-paediatric-diabetes-audit

A django application to audit the care of children and young people with diabetes in England and Wales.
0 stars 1 forks source link

denormalize user #276

Open eatyourpeas opened 6 days ago

eatyourpeas commented 6 days ago

Currently if a user uploads a CSV, the rows in it are validated against the relevant model, errors collected of the affected fields and a record stored for each in relevant model. This works particularly for the visit model (which has no unique identifier) but not so much for the Patient model which complains if there are two instances of NHS number which currently has unique=True in the model.

If a user uploads the same patient in the same submission, the new csv will be saved, the old csv, patient and visit records are deleted and the new records saved. A record of the upload remains in the submission table, but it is inactive and all related csvs and patient data have been removed.

However, if someone else, in a different PDU creates a new submission which is active but contains the same patient, this will generate an error at database level (since NHS number is unique) and the submission will be saved but none of the patients.

This highlights a fundamental issue possibly about the data model and versioning of our records. One way round this would be to allow multiple records with the same NHS number in the Patient table, but not allow more than one record per Patient in the Transfer table in a given active submission. Possibly we need to chat about this.

mbarton commented 1 day ago

Not quite sure how to model this in the Django ORM but I think it makes sense for patients to be unique by NHS number within a given submission. The CSV upload code ensures this at the moment as it groups by NHS number before validating the patient fields but we'd probably need something for the form submission that checks it.

Allowing multiple patients this way also solves the problem of how we model a patient postcode or GP details changing over time.