Implementing Unique Constraints in Aidbox: Ensuring SSN Uniqueness

Evgeny Mukha
April 11, 2025
5 min

The problem: Your storage system must ensure that the SSN identifier is unique across all patient records. This means that if you try to create a new patient with an SSN and another patient with the same SSN already exists, the system will reject the request to prevent duplicate records.

To solve this problem in the Aidbox FHIR Server, you can create an index that enforces this rule:

CREATE UNIQUE INDEX patient_email_unique_idx1
ON patient ((jsonb_path_query_first(resource, '$.identifier[*] ? (@.system == "http://hl7.org/fhir/sid/us-ssn").value') #>> '{}'));

Explanation:

  • CREATE UNIQUE INDEX patient_ssn_unique_idx1
    This defines a unique index named `patient_ssn_unique_idx1`. When you create a unique index, PostgreSQL prevents duplicate values in the specified column(s). If a duplicate value is inserted, the database will throw a unique constraint violation error.
  • ON patient
    The index applies to the patient table.
  • (jsonb_path_query_first(resource, '$.identifier[*] ? (@.system == "http://hl7.org/fhir/sid/us-ssn").value') #>> '{}')
    • resource: Refers to the JSONB column storing patient data.
    • jsonb_path_query_first(...): Extracts the first identifier that meets the condition.
    • '$.identifier[*] ? (@.system == "http://hl7.org/fhir/sid/us-ssn").value':
      • Searches for all identifier elements in the resource JSON.
      • Filters identifiers where the system is "http://hl7.org/fhir/sid/us-ssn" (indicating an SSN).
      • Extracts the value field, which contains the actual SSN.
    • #>> '{}': Converts the extracted JSONB result into a plain text value for indexing.

Test it on your local FHIR server.

How did you like the article?
Be the first to know!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.

contact us

Get in touch with us today!

By submitting the form you agree to Privacy Policy and Cookie Policy.
Thank you!
We’ll be in touch soon.

In the meantime, you can:
Oops! Something went wrong while submitting the form.

Never miss a thing
Subscribe for more content!

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
By clicking “Subscribe” you agree to Health Samurai Privacy Policy and consent to Health Samurai using your contact data for newsletter purposes