A hierarchical relation (sometimes called a parent-child) is a convenient way of organizing and viewing information in a one-to-many fashion.
If you think about it, we use it all the time:
A parent-child relation is added in a similar way as fields and lookup relations. In "Developer Mode", click on the collection you want to add children to, then click the Add Field+ button. In the example below, we want to add Activities to a Contact so that we can log all interaction with this person.
After we've given a name to our new relation, we scroll down below all the basic data types and down to the section Relations to Collections and click on "activities". We will then select between "select one", "select many" and "child list of activities". The last option (child list) makes this a parent-child relation.
Switch off "Developer Mode" (or just click the Contact menu item) to see the changes in our Contact collection. In the list view we'll see an expand arrow next to each row indicating that there is one (or more) child relations. This looks like the screenshot below.
Clicking "New" on the Activities child list menu will let us add a new Activity child record to the current Contact record.
After saving the new activity, we can navigate up to the parent using the link right below the headline ("Child of....").
Storing a child record has no effect on the parent record data fields (except some metadata).
Instead the child record gets 3 meta data fields (_parent_id, _parent_def, _parent_field) as a reference to its parent:
{
"_id": "58120fcf7bb8670200000005",
"description": "Discussed possible merger with AcmeCorp",
"files": [
"58120fee7bb8670200000006"
],
"_parent_id": "5811c44bf463f46c000015a3",
"_parent_def": "contact",
"_parent_field": "activities",
"_created": "2016-10-27T14:31:43.285Z",
"_changed": "2016-10-27T14:32:29.035Z",
"_createdby": "knutmt@restdb.io",
"_changedby": "knutmt@restdb.io",
"_keywords": [
"knutmt",
"restdb",
"io",
"discussed",
"possible",
"merger",
"with",
"acmecorp"
],
"_tags": "knutmt restdb io discussed possible merger with acmecorp",
"_version": 1
}
Note that these fields are only visible if you do a REST API call using the query parameter: ?metafields=true
https://simpledb-abee.restdb.io/rest/activities/58120fcf7bb8670200000005?metafields=true
To access children of a record using the REST API, you don't have to compose a query using the parent_xxx
metafields mentioned previously. You can just formulate the query like this:
https://simpledb-abee.restdb.io/rest/contact/5811c44bf463f46c000015a3/activities
This will give you alle the activities children of the contact with id "5811c44bf463f46c000015a3"
. You can also refer directly to a specific child as shown below following common REST API conventions. Accessing the activity directly is more compact however (/activities/(id)).
https://simpledb-abee.restdb.io/rest/contact/5811c44bf463f46c000015a3/activities/58120fcf7bb8670200000005
Read our REST API docs for more information.