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..e9e437138 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,14 @@ 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', + ), + ] class ContactInformation(PatientSubrecord, ExternallySourcedModel):