To create a relationship between two tables, you must have a column with identical values in both tables.
Relations can be used in Glide to create relationships between rows, linking together their data across tables.
About relations
In a Glide Project, every row in your tables represents an item that then appears in the lists and components in your project.
Each item has its own properties. These are represented as columns in the same table. For example:
- A
Room
can have a:Number
,Floor
,Extension
,Desks
,Image
- An
Employee
can have a:Name
,Number
,Email
,Image
- A
Product
can have a:Name
,SKU
,Image
,Price
,Description
In some cases, an item has its own item. These are represented as columns across multiple tables. For example:
- A
Room
has aFloor
- An
Employee
has aManager
and anOffice
- A
Product
has aCategory
When an item has its own item, they each have their own set of properties and exist as independent rows in tables. They can then be connected by building relations.
Relations fall into two categories - single relations and multiple relations.
Single relations
Single relations show a one-to-one relationship. This is where one item relates to one other item.
Here are three examples of apps where items in one area relate to items in another.
- In the company app (left),
Staff
haveLocations
. - In the conference app (middle),
Talks
haveSpeakers
- In the blogging app (right),
Articles
haveAuthors
.
Multiple relations
Multiple relations show a one-to-many relationship. This is where one item relates to a list of other items.
For example:
- In the staff directory (
Staff
table), eachLocation
lists all of theStaff
that work there. - The
Manager
shows a list of all theStaff
they manage.
How to create relations in Glide
In Glide, you can create relations between values using a Relation column. With this column, you specify the type of relation you want - single or multiple. Your project then applies the relation to the values and tables specified.
Check for matching values
Before adding a Relation column, you must first check that the values match in the columns that will be connected. Items cannot be linked together unless they match.
In this example, a relation can be created for an employee directory that has a Staff
table and Locations
table. Each employee in Staff
has a Location
they work in.
By connecting this information, you can integrate it into components or even link to their location in their profile.
- In the
Staff
table, make sure there's a column for the name of the employee'sLocation
. - In the
Locations
table, make sure there's a column for the name of the location that can be tied to theStaff
table.
Even though these columns have different names in each table, the values in them are shared.
After determining that there are matching values, you can build single or multiple relations using the Relation column.
Build single relations
In this example, you can create one-to-one relations by bringing the Location
for each employee into the Staff
table.
- In the Data Editor, go to the table you want to add the column to.
- Add a new column by clicking the plus symbol to the right of the table. The plus symbol will append the column to the end of the table. To place the new column in a specific order, click on the dropdown button in the column header next to where you want to place it and then click Add column right.
- Enter a name for the column.
- In the Type dropdown, select Relation from the Computed section.
- In the Configuration section, specify the values to be matched. The relation is made when the value in the first dropdown matches the value in the second dropdown.
- Click on Done to add the new column to the table.
Build multiple relations
In this example, you can create one-to-many relations by bringing each employee from the Staff
table into the Location
table next to their matching locations.
- Add a new Relation column using the instructions in Build single relations.
- In the Configuration section, specify the values to be matched.
- Enable the Match multiple option.
- Click on Done to add the new column to the table.
Using relations
The most common use for a Relation column in Glide is to display a single related item or list of items.
- In Apps, this is done with the Relation component, Inline List, and List Relation.
- In Pages, this is all done with Collections.
With that said, you may find more advanced ways to use multiple relations - such as in Choice components or as the source for the data displayed in Charts.
Learn more about this in the video below.