entity_relationship.json

Another important component of OpenSRP forms is entity_relationship.json. There is one entity_relationship.json per application (in this case OpenSRP). As the name suggests, it defines the relationship between the various entities of the app. The entities that are part of the app as of now are household, eligible_couple, mother and child. The entity_relationship.json is below:

entity_relationship.json
[    {
        "parent": "household",
        "child": "eligible_couple",
        "field": "children",
        "kind": "one_to_many",
        "from": "household.id",
        "to": "eligible_couple.relationalid"
    },
    {
        "parent": "eligible_couple",
        "child": "mother",
        "field": "wife",
        "kind": "one_to_one",
        "from": "eligible_couple.id",
        "to": "mother.ecCaseId"
    },
    {
        "parent": "mother",
        "child": "child",
        "field": "children",
        "kind": "one_to_many",
        "from": "mother.id",
        "to": "child.motherCaseId"
    }
]

Each entry in list defines a relation. We have three relationships in OpenSRP, one_to_many between household & eligible_couple, one_to_one between eligible_couple and mother and one_to_many between mother and child. Explanation for the fields below:

parent: This specifies the parent entity in the given relation.
child: This specifies the child entity in the given relation.
field: This specifies the field on parent entity which allows us to load child entity for that parent. This was added to support the cases where entities are stored on Document based Databases like CouchDB. This is currently not used or implemented in the app.
kind: This specifies the type of relationship. Currently supported valyes are one_to_one and one_to_many.
from and to: Together these two values specify how to load parent and child entities from a relational database.

 

BRIEF Explanation: We have Household as an entity which is the parent of an eligible couple. This is a one to many relationship; which is specified by “kind” field in the object. The most interesting part here is “from” and “to”. The client would have a unique code as “id”  for each household and every other entity (not to be confused with an household id in questionnaire).

According to the sample entity_relationship.json posted above , the household.id (auto generated entity id not to be confused with an household id in a questionnaire) would automatically be the eligible_couple.relationalid as it is mentioned in the lines:

"from": "household.id",

"to": "eligible_couple.relationalid"

This is how OpenSRP keeps track of parent and children relationships of entities.