From 181de3434a91a9c303d43a72b8737cb80b3e3bee Mon Sep 17 00:00:00 2001 From: fredkingham Date: Thu, 23 Mar 2023 17:20:40 +0000 Subject: [PATCH 1/2] Adds the non zero prefixed MRN database constraint Adds a database constraint to make it so that it is impossible to change an MRN so that it has a zero prefix. --- elcid/migrations/0069_auto_20230323_1653.py | 17 +++++++++++++++++ elcid/models.py | 12 +++++++++--- 2 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 elcid/migrations/0069_auto_20230323_1653.py diff --git a/elcid/migrations/0069_auto_20230323_1653.py b/elcid/migrations/0069_auto_20230323_1653.py new file mode 100644 index 000000000..8426a8fbd --- /dev/null +++ b/elcid/migrations/0069_auto_20230323_1653.py @@ -0,0 +1,17 @@ +# Generated by Django 2.2.16 on 2023-03-23 16:53 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('elcid', '0068_remove_mergedmrn_upstream_merge_datetime'), + ] + + operations = [ + migrations.AddConstraint( + model_name='demographics', + constraint=models.CheckConstraint(check=models.Q(_negated=True, hospital_number__startswith=0), name='No initial zeros'), + ), + ] diff --git a/elcid/models.py b/elcid/models.py index 822a412df..089a5c4e9 100644 --- a/elcid/models.py +++ b/elcid/models.py @@ -42,11 +42,11 @@ class MergedMRN(models.Model): class PreviousMRN(models.Model): """ - A mixin for subrecords to maintain an audit trail for occasions + A mixin for subrecords to maintain an audit trail for occasions when an upstream MRN merge occurs and the merged MRN has elCID entries. - + `previous_mrn` is the MRN in use at the time that this subrecord instance - was last created/edited with if that MRN is different from the current + was last created/edited with if that MRN is different from the current value of `Demographics.hospital_number` attached to this instance. """ previous_mrn = models.CharField(blank=True, null=True, max_length=256) @@ -75,6 +75,12 @@ def age(self): class Meta: verbose_name_plural = "Demographics" + constraints = [ + models.CheckConstraint( + check = ~models.Q(hospital_number__startswith=0), + name = 'No initial zeros', + ), + ] class ContactInformation(PatientSubrecord, ExternallySourcedModel): From 96b31db47024f9e2458e2674e48793d8dbfe3ca4 Mon Sep 17 00:00:00 2001 From: fredkingham Date: Wed, 26 Apr 2023 10:20:28 +0100 Subject: [PATCH 2/2] Adds a comment to elcid models Negation of a Q expression is done with a tilda ('~') this looks wrong at first glance so a comment has added with a link to the django documentation to explain the syntax. --- elcid/models.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/elcid/models.py b/elcid/models.py index 089a5c4e9..e9e437138 100644 --- a/elcid/models.py +++ b/elcid/models.py @@ -76,6 +76,8 @@ def age(self): class Meta: verbose_name_plural = "Demographics" constraints = [ + # The bitwise "not" here is actually how you do the negation of a Q expression + # https://docs.djangoproject.com/en/dev/topics/db/queries/#complex-lookups-with-q-objects models.CheckConstraint( check = ~models.Q(hospital_number__startswith=0), name = 'No initial zeros',