Once you have an app with multiple tables, it's often useful to create connections, known as references, between the app's tables.
For example, an order capture app might contain the following tables:
Orders, with one row per order.
Order Details, with one row per line item.
Products, with one row per product being sold.
Customers, with one row per customer.
The order capture app might contain the following references:
Ordersrow will reference the
Customersrow of the customer who placed that order.
Order Detailsrow will reference its parent
Order Detailsrow will reference its corresponding
References serve three purposes:
- They allow you to represent relationships. For example, the reference between the
Ordersrow and the
Customersrow allows you to capture the relationship between an order and the customer who placed that order.
- They allow you to easily retrieve information from a related row in another table. For example, the reference between the
Order Detailsrow and the
Productstable allows you to start from an individual
Order Detailsrow and retrieve the name, image, and price column values from the related
- They allow you to navigate from one row to another. For example, you might retrieve a particular
Order Detailrow then navigate to the related
Ordersrows, and then to the related
After you define a reference, to get a column value from a referenced table use dereference expressions.
The following sections provide more details about references between tables:
- Get started with references
- Create references
- Reverse references
- References and reverse references contain key values
- Express ownership between tables
Get started with references by watching a video or using a sample app, as described below.
Watch the following video to learn how to create references between tables with AppSheet.
Get started using references between tables in an app by using the Order capture how-to feature sample.
A reference can be added to a table in two ways.
- AppSheet automatically adds references when you generate or regenerate a table (system-added)
- You can manually add references (user-added)
When you initially create your app or when you regenerate a table's column structure, AppSheet will try to automatically infer references between tables. For example, if you have a
Customers table with a
Name column as its key, and if the
Orders table has a column called
Customer Name, the
Customer Name column is assumed to be a
You create a reference by adding a column of type
Ref to a table and specifying the related table's name. For example, in the
Orders table you would add a
Ref to the
Customers table. In the
Order Details table you would add one
Ref to the
Orders table and another to the
You can add a reference as follows:
- Ensure your worksheet has a column to contain the reference.
- If you needed to add a new worksheet column to contain the reference, regenerate the table schema. This will include the newly added worksheet column in the table.
- Go to Data and select the table you want to edit in the list.
If you are using the legacy editorGo to Data > Columns and expand the table you want to edit.
- In the Type drop-down, select Ref.
- In the Source table drop-down, select the referenced table.
- Click Done.
- Save your changes by selecting one of the following:
- Save - Save the app.
- Save & verify data - Save the app and verify that it is runnable based on external dependencies.
Ref you add, the system automatically adds a reverse reference in the opposite direction. This is true for both user-added and system-added references. The reverse reference virtual column is given a default name that you can change.
For example, when you add the
Ref from the
Orders table to the
Customers table, the system automatically adds a reverse reference from the
Customers table to the
Reverse references serve three purposes:
- They allow you to navigate from one row to all of its related rows.
- They allow the user interface to easily display a row along with all of its related rows in another table.
- They allow aggregates to be computed like the count of a customer's orders or the total dollar value of a customer's orders.
For both references and reverse references, reference icons are displayed.
Ref column always stores the key column value of the referenced row. For example, if the key column value of a
Customers row is
Ann Adams then the
Ref field in the related
Orders row will contain the value
Ann Adams. A table's key column value uniquely identifies each row in that table. The copy of the key column value in the
Ref column allows the system to unambiguously retrieve the proper row in the referenced table. See What is a key?
The system-added reverse reference column is a list of the key column values of the related rows. For example, the reverse reference in
Ann Adams would contain
1010 if those were the key column values of Ann's related
Orders records. When viewing an individual row, related reverse references appear as inline views that can be customized.
References can indicate not only that two tables are related, but that rows of one table should be owned by (or considered a part of) rows from another table. This is done by activating the IsAPartOf option in the
Ref column structure. Typically, this should only be done in cases where rows containing the
Ref column only make sense when associated with a row from the referenced table. For example, you may have a separate
Order Details table for line items that reference an
Order, but conceptually each entry should be considered part of an
Order and shouldn't exist independently (put another way, the
Order record owns the
Order Details that reference it).
There are several implications of enabling this ownership relationship:
- Form views should allow users to add or update related rows that are a part of that row without leaving the form.
- A form comprised of multiple rows should be treated as a single update.
- If a row is deleted, any related rows that are a part of it should also be deleted (and again treated as a single update).
The first of these to take effect will be #1: System-generated reverse reference columns related to
Ref columns marked
IsAPartOf will appear in form views and allow users to view, add, and edit line items within the form view. The availability of these options is limited by the table or slice permissions (and until the final form is saved, pending adds can still be modified even if the table doesn't allow updates).
There are some limitations of which to be aware:
- A table can only have one
IsAPartOf(a row can only be a part of one other row).
- Actions are not supported within forms. Inline views based on reverse references in forms will not display actions or trigger actions based on view events.
- Canceling a form with pending line item adds/edits also cancels those adds/edits.