Help - Establish a relationship between programs

Hi @Scott,

I am using DHIS 2.34.4

I wish to create 2 programs - Household and Beneficiary using 2 different tracked entity types: Household and Beneficiary respectively. I have the following questions regarding relationships and analytics:

  1. To establish a relationship between Household and Beneficiary, do I have to create two relationship types as shown in demo between mother and child: Mother-Child_a-to-b_(Person-Person) and Mother-Child_b-to-a_(Person-Person)?

a) Why two relationship types and not one. I am bit confused.
b) To link data between Household and beneficiary, I would like to have Household ID as a foreign key in beneficiary. In other words, I would like to be able to first select the Household ID and register all the contacts. How can I configure this?

  1. When it comes to analytics, will it be possible in the above DHIS 2 version to view all the Beneficiaries in a particular household in a single report?

Thanks

Hi @hernandezmachava ,

You only need a single relationshipType for your case. You choose Household as one side, and Beneficiary as the other side. You can furthermore define the relationshipType as either bidirectional or not. Bidirectional relationships means the relationship is seen from both sides, which also means users need write access to both sides of the relationship.

The relationships you see in the demo, is a result of a version upgrade where we introduced a new form of relationship, where we had to break up single relationships into multiple relationships. You can disregard those.

We do not work with foreign keys in tracker\relationships, so if you are asking in the context of a relationship, you just need to pick the two tracked entities in questions, namely the Household and the Beneficiary you want to relate. If you are asking in the context outside of relationships, you would need to add a tracked entity attribute to the Beneficiary that represents Household ID. In the latter case, users would have to manually add the value - There is no support for creating dropdowns or searching for existing data in forms.

For your second question regarding analytics, there is currently no support for relationships in analytics, so I don’t think what you are asking is supported. However, you can through the API get that information, and export it. I’m not sure that will solve your problem, but that is a current workaround for getting this data in the manner you are asking.

Hope this helps :slight_smile:

1 Like

Hi @Stian,

I am really happy with your explanation. About the inherit function in the configuration page, is it used to inherit attributes that will link both tracked entities?

With regard to this part:

How would the API look like that would help me get the information related to households and their beneficiaries? I have read about API, but I am still not good at it. I am seeking guidance.

Thanks again.

1 Like

There are two ways currently you can use the API to get related data:

  1. trackedEntityInstances endpoint
/api/trackedEntityInstances/
/api/trackedEntityInstances/<uid>
  1. Relationships endpoint
/api/relationships?tei=<uid>

Option 1 will return trackedEntityInstance(s) and related data, while option 2 will return all relationships for a given trackedEntity.

Have a look at the documentation and play around with the API to see if you find a good representation for you; If not, I would suggest writing a jira issue with your use-case so we can look into if or how we can support it :slight_smile:

1 Like

Hi @Stian,

Sorry if I am asking a lot of questions. Does the uid in both 1 and 2 represent the UID of either household or beneficiary?

I will look at the documentation about the API.

Thanks so much

1 Like

In theory, a relationship has 2 distinct sides. This way we can say that one side is a Household and one is a Beneficiary. This is how we store them in the database. However, if you choose a bidirectional relationship, the sides doesnt matter, as long as either side is a Household and the other a Beneficiary :slight_smile:

1 Like

Hi @Stian,

Thanks so much for your detailed explanation! It is very helpful.

Thanks

1 Like