Search…
One-to-many relationships

Introduction

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.

Setup

To get a one-to-many relationship setup you just need:
    1.
    A parent object (In this case we'll use Account for the patient).
    2.
    A child object (in this case we've already created the Prescription object).
    3.
    A Reference or ObjectId property on the Prescription object with its Source Object set to Account and indexed set to true
    4.
    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 there's 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.

ObjectId

From the Property Type list, we'll select ObjectId. Then we'll be presented with more options particular to the ObjectId property.
    1.
    For Source Object we'll select "Account" and we will select Indexed.
    2.
    Then client Create Property.
You could also select Reference as the property type and set the same values for Source Object and Indexed. ObjectId properties are lighter weight, but Reference properties have the advantage of allowing expansion into the referenced object instance.

List

After creating the Patient ObjectId property, 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 return a list of Prescriptions related to this patient Account.
The only other option we need to set is the "Linked Property" option. In this case, set it to the Patient (c_patient) property we created a moment ago. Then save the new list property.
Ultimately what this list equates to is something similar to the following query:
1
GET /c_prescriptions?where={"c_patient":"575f58281d0c03a53ccc3ac6"}
Copied!
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 prescriptions right in the response with our accounts like so:
Request
Response
1
GET /accounts?include=c_prescriptions
Copied!
1
{
2
"data": [
3
{
4
"_id": "575f58281d0c03a53ccc3ac6",
5
"access": 6,
6
"c_prescriptions": {
7
"data": [
8
{
9
"_id": "577c883c6f3035ee08357dfa",
10
"access": 7,
11
"c_date": "2016-07-01",
12
"c_dispense": 60,
13
"c_patient": {
14
"_id": "575f58281d0c03a53ccc3ac6",
15
"object": "account",
16
"path": "/accounts/575f58281d0c03a53ccc3ac6"
17
},
18
"c_provider": {
19
"_id": "5771495a1d0c03a53ce83f1a",
20
"object": "account",
21
"path": "/accounts/5771495a1d0c03a53ce83f1a"
22
},
23
"c_refills": 3,
24
"c_rx": "Doxycycline 100mg tab. Take one PO bid.",
25
"created": "2016-07-06T04:25:32.558Z",
26
"creator": {
27
"_id": "575f58281d0c03a53ccc3ac6",
28
"object": "account",
29
"path": "/accounts/575f58281d0c03a53ccc3ac6"
30
},
31
"favorite": false,
32
"object": "c_prescription",
33
"owner": {
34
"_id": "575f58281d0c03a53ccc3ac6",
35
"object": "account",
36
"path": "/accounts/575f58281d0c03a53ccc3ac6"
37
},
38
"shared": false
39
}
40
],
41
"hasMore": false,
42
"object": "list"
43
},
44
"created": "2016-06-14T01:04:40.888Z",
45
"email": "[email protected]",
46
"favorite": false,
47
"key": {
48
"fingerprint": "73ad85a0-31cc-11e6-ba07-3f24c7443557",
49
"secret": "PXXvBvG86sJC2RVk4X6K4FV0OUH7ojzp"
50
},
51
"locale": "en_US",
52
"locked": false,
53
"mobile": "+15555555555",
54
"name": {
55
"first": "John",
56
"last": "Smith"
57
},
58
"object": "account",
59
"roles": [
60
"000000000000000000000004",
61
"000000000000000000000007",
62
"000000000000000000000006"
63
],
64
"shared": false,
65
"state": "verified",
66
"updated": "2016-06-14T01:08:06.647Z",
67
"updater": {
68
"_id": "575f58281d0c03a53ccc3ac6",
69
"object": "account",
70
"path": "/accounts/575f58281d0c03a53ccc3ac6"
71
}
72
}
73
],
74
"hasMore": false,
75
"object": "list"
76
}
Copied!
Last modified 1mo ago