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 through 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, select "Account" and we will select Indexed.

  2. Click 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:

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 prescriptions right in the response with our accounts like so:

GET /accounts?include=c_prescriptions

Last updated