Referencing Other Resources

FHIR resources rarely exist in isolation, they often reference one another in order to capture real-world healthcare relationships. For example, an Observation (e.g. a lab result) points to the Patient it belongs to.

In this tutorial, we will explore how references work in FHIR and why referential integrity matters. Using requests to a live server you will,

  • Create an Observation that references a Patient
  • Understand what happens if the referenced Patient doesn’t exist
  • Correctly link the Observation to a valid Patient ID
  • See how referential integrity prevents deletion of resources in use

What is referential integrity?

Referential integrity ensures that references between FHIR resources remain valid. Most FHIR servers enforce this principle.

This would mean that, for example, you can’t create an Observation that is not linked to a Patient, or delete a Patient that has other resources (Observations Encounters, etc.) referencing it.

This prevents the occurrence of ‘broken links’, ensuring that records are consistent and available.

Step 1: Prepare a Patient

  1. Create a patient resource on the FHIR server using the instructions in the previous lesson.
  2. Copy the Patient ID returned by the server. Use the id field, not identifier
  3. Access your patient resource with the below command to confirm. Please replace {id} with the ID of your patient.
GET https://fhir-bootcamp.medblocks.com/fhir/Patient/{id}

Here is a sample JSON you can edit and use for creating a new patient.

{
   "resourceType": "Patient",
   "name":[
       {
       "given": ["Sidharth"],
       "family": "Ramesh"
       }
   ],
   "telecom": [
       {
           "system": "email",
           "value": "sidharth@medblocks.com"

       }
   ]
}

Step 2: Create an Observation

Here’s a sample Observation resource for an HbA1c blood test. You can use this to create your own Observation.

{
   "resourceType": "Observation",
   "status": "final",
   "code": {
       "coding": [
           {
               "system": "http://loinc.org",
               "code": "4548-4",
               "display": "Hemoglobin A1c/Hemoglobin.total in Blood"
           }
       ]
   },
   "valueQuantity": {
       "value": 6.3,
       "unit": "%",
       "system": "http://unitsofmeasure.org",
       "code": "%"
   },
   "subject": {
       "reference": "Patient/123"
   }
}

In Postman, set up a POST request,

POST jttps://fhir-bootcamp.medblocks.com/fhir/Observation

Paste the Observation above to Body —> Raw —> JSON.

Step 3: Post with a non-existent Patient reference

If you try to post this Observation with a non-existent Patient ID, e.g. Patient/123, the server responds with an error that includes,

Resource Patient/123 not found, specified in path: Observation.subject

This happens because the FHIR server enforces referential integrity. It doesn’t allow an Observation to point to a Patient that doesn’t exist.

Step 4: Post the Observation with a valid Patient ID

Next, replace Patient/123 with the actual Patient ID you received after Step 1.

Send the POST request again. This time, the Observation is created successfully and correctly references the Patient.

Step 5: Trying to delete the patient

Now, let us attempt to delete the Patient resource.

DELETE https://fhir-bootcamp.medblocks.com/fhir/Patient/{id}

The server responds with an error similar to this,

Patient cannot be deleted. Resource is referenced by Observation/{id}.

In this case, you will need to first delete dependent Observations before deleting the Patient itself.

This prevents accidental data loss.

Summary

Referential integrity is an important aspect to consider when working with FHIR resources. In this lesson, we see referential integrity demonstrated by using Patient and Observation resources. So,

  • An Observation must reference a valid Patient
  • A Patient resource that is referenced cannot be deleted

Comments (0)

No comments yet. Be the first to comment!