One-to-Many Relationships
Modeling a One-to-Many Relationship
As we've seen already when creating a custom object , you can relate one object to another with a Reference
property type. That creates just one side (the many) of the one-to-many relationship, however. From the parent object, how do we get the list of the many child objects from the relationship? In this how-to, we'll walk though modeling that out completely by adding a List
property.
To get a one-to-many relationship setup you just need:
- A parent object (In this case we'll use Account for the patient)
- A child object (in this case we've already created the Prescription object
- A Reference property on the Prescription object that points to the Account object
- A List property on the Account object that provides the list of related Prescriptions when getting the Account.
Now, the Account object is already there so nothing to do with it just yet. Let's start with the child object, in this case the "Prescription" object:
If you haven't already, create your Prescription object by following these steps.
If you haven't done so already, create the Patient
property. Just scroll down below the list of standard properties and click on the New Property
button.

You'll then be presented with the new property screen.

From the Property Type
list, we'll select Reference
. Then we'll be presented with more options particular to the reference property.

For Source Object
we'll select "Account" because we want to relate the prescription to the patient's account record. For Required Access
we'll select "Connected". This means that whoever is creating the Prescription will need to have at least that level of access to the patient's account record. For Expandable
we'll leave it as selected. For Grant Access
we'll select "Read". Lastly, we'll select Indexed
. This will allow us to query prescriptions by patient account _id. Then client Create Property
.
After creating the Patient account, you will see it appear in the Custom Properties
List. Now that that is in place, we need to create the list property on the account. Head back to Settings > Objects and click on the "Account" object. Then scroll down and click "New Property".

Name it "Prescriptions" and select "List" as the property type and "Prescription" as the source object. We give it a plural name because this property will always return a list of (many) Prescriptions.
For Grant Access
, choose "Read". This provides for read access to the prescriptions for any user that has access to the list property. This could be important for, say, a Pharmacist who may be connected to the Patient but may not necessarily have the right level of access to all of the Prescriptions. Setting the grant in this property takes care of that automatically.
Then the next step is to define the matching clause. This is important because this property is basically a virtual property and the matching clause acts to filter that list down to the proper items. In this case, the proper patients to show are those whose "c_provider" property (the reference we created above) is equal to this providers account. So to do that, we add in a where clause like so:
{"c_patient":"{{input._id}}"}
that c_provider is the property on the Patient object we just created. The input._id is a variable that is getting passed into the matcher. It is the _id of the parent account. You'll notice that it's included with double curly braces around it. This is mustache notation and is what tells the matcher that this is a variable to pass in. In runtime, this will equate to something like:
{"c_patient":"575f58281d0c03a53ccc3ac6"}
Ultimately what this matcher equates to is the same as doing the following query:
GET /c_prescriptions?where={"c_patient":"575f58281d0c03a53ccc3ac6"}
But as a list property, we don't have to make that separate request, we can just get our accounts and pass in an ?include=c_prescriptions and we'll get the property in the response!
Like so:
GET /accounts?include=c_prescriptions
Response:
{
"data": [
{
"_id": "575f58281d0c03a53ccc3ac6",
"access": 6,
"c_prescriptions": {
"data": [
{
"_id": "577c883c6f3035ee08357dfa",
"access": 7,
"c_date": "2016-07-01",
"c_dispense": 60,
"c_patient": {
"_id": "575f58281d0c03a53ccc3ac6",
"object": "account",
"path": "/accounts/575f58281d0c03a53ccc3ac6"
},
"c_provider": {
"_id": "5771495a1d0c03a53ce83f1a",
"object": "account",
"path": "/accounts/5771495a1d0c03a53ce83f1a"
},
"c_refills": 3,
"c_rx": "Doxycycline 100mg tab. Take one PO bid.",
"created": "2016-07-06T04:25:32.558Z",
"creator": {
"_id": "575f58281d0c03a53ccc3ac6",
"object": "account",
"path": "/accounts/575f58281d0c03a53ccc3ac6"
},
"favorite": false,
"object": "c_prescription",
"owner": {
"_id": "575f58281d0c03a53ccc3ac6",
"object": "account",
"path": "/accounts/575f58281d0c03a53ccc3ac6"
},
"shared": false
}
],
"hasMore": false,
"object": "list"
},
"created": "2016-06-14T01:04:40.888Z",
"email": "[email protected]",
"favorite": false,
"key": {
"fingerprint": "73ad85a0-31cc-11e6-ba07-3f24c7443557",
"secret": "PXXvBvG86sJC2RVk4X6K4FV0OUH7ojzp"
},
"locale": "en_US",
"locked": false,
"mobile": "+15555555555",
"name": {
"first": "John",
"last": "Smith"
},
"object": "account",
"roles": [
"000000000000000000000004",
"000000000000000000000007",
"000000000000000000000006"
],
"shared": false,
"state": "verified",
"updated": "2016-06-14T01:08:06.647Z",
"updater": {
"_id": "575f58281d0c03a53ccc3ac6",
"object": "account",
"path": "/accounts/575f58281d0c03a53ccc3ac6"
}
}
],
"hasMore": false,
"object": "list"
}
Updated 4 months ago