Talk:Proposed Data Model Changes


Patient/Patient relationships

I am trying to wrap my mind around the data model. Can someone provide an example SQL query that returns a list of patients that are related to a specific patient?

Answer
Relationships go in either direction. So the patient_id (which equals their person_id) can be in either the person_id or the relative_id column.

select 
  pat.patient_id, 
  rel.person_id, 
  rel.relative_id 
from 
  patient pat,
  relationship rel
where
  pat.patient_id = rel.person_id or
  pat.patient_id = rel.relative_id;

Better Yet...use the API

List<Relationship> relations = Context.getPersonService().getRelationshipsByPerson(somePatient);