Table of Contents

About Connections

Lesley Updated by Lesley

What is a Connection? 

Most apps will need more than one table. When this is the case, these tables will likely be related or connected to each other.

Here are some examples of when connections can be used:

  • Many contacts can be connected to one company
  • One order can be connected to many products
  • Many managers can be connected to many projects

Knack defines these relationships with connections. Adding a connection means that records from one table can be connected to records from another table. These connections can be used throughout your app to add powerful functionality.

To use connections, you will need to have at least two tables. If you want to learn more about how to use a table, you can read more here.

Connection Example 

In the contact directory example app, we have two tables: Companies and Contacts. Each contact record is connected to a company record.

This means we can view a company record and add a view to show all the contact records connected to that company record.

The same app also has a Notes table, where each note record is connected to a contact record. This enables us to show notes records connected to each contact record.

To see more examples of how connections can be used in Knack, see this guide.

Manage Connections

In the Data section of the Builder when viewing an table's fields, each table has a Connections section along the right side of the Builder. Here you can add new connections to the table, as well as view any existing connection fields.

Add a Connection

To add a connection field, select a table in the Data section of the Builder to view the fields list for that table. Once there, you can use the green "+" button in the right column to add a new connection field.

This will present a new window where you select the table you want the connection to connect to:

Once you have selected the table for your connection, next you define how that connection will work. When defining how the connection will work, you can choose from three main connection types, one-to-one, one-to-many and many-to-many.

For example, here are what these connection types would look like when using the tables Contact and Company.

  • One-to-One: a connection where each Contact record connects to one Company record and each Company record connects to one Contact records.
  • One-to-Many: a connection where each Contact record connects to one Company record and each Company record connects to many Contact records.
  • Many-to-Many: a connection where each Contact record connects to many Company records and each Company record connects to many Contact records.

Once you have selected which connection type to use, click "Add Connection" and a connection will be added to the table. When you add a new connection, it creates a special connection field in that table:

These fields can be used like any other field throughout your app. If you add a connection field to a form, it will operate like a lookup, so you can select a specific record from the connected table to connect to.

Which Table to Add Your Connection To

You only need to add one connection field to create a connection between two tables, so which table should you add that field to?

For one-to-one and many-to-many relationships, we suggest placing the connection field on whichever table will be used or edited more frequently. This will affect where the connection field lookup will appear and make it easier to edit with a form view.

In a one-to-many relationship, in most cases, it is best practice to add the connection to the table with many connections. Or to put it another way, in a parent-child relationship, it works best to add the connection to the child table. 

For example, in our Job Listings example app, one Job can connect with many Applications, so it will work best to add the connection to the Application table. This way, when we're editing or viewing an Application record, we can easily see which Job it’s connected to.

Here's what that connection will look like in a form when it's added to the Application table (or the child table). The connection field to the Job table will appear as a lookup field:

In this example, the application is connected to the "Lead Engineer" Job. You can change which Job it is connected to with the lookup (also called drop-down) field.

Next, here's what it would look like if the connection field was added to the Job table (or the parent table). Instead of connecting to one record it would need to connect to many or all the Application records:

Another reason the child table generally works best is that you'll typically create the parent records first. So when adding a child record, you'll have some parent records already created that you can connect to.

Edit a Connection

To make changes to an existing connection field, navigate to the Data section of the Builder and locate the connection in the table's fields. Click on the "..." more options icon to choose the field settings.

Or open the field options and select the "Edit field settings" option while viewing the records in the Records section of the Builder:

Next, a popup with the connection field options will open.

Here you will be able to change the connection type as well as other basic field settings:

  • Name: This changes the name of the field in your Builder.
  • Description: This is a description which can be used to describe the field internally. These will show up as tooltips when viewing records in the Records section of the Builder.
  • Required: This makes the field a required field.
  • Default: Choose the default option that will display in your connection field when live.
  • Connection: Change the connection type.
  • Format: Choose between connection values displaying in a searchable dropdown or as radio buttons in your Live App.
Editing a connection field can corrupt existing connections between tables and corrupt views which use this connection. Be cautious about editing a connection field. 

Delete a Connection 

Deleting connections should be done with caution because they are the basic building blocks in your app's foundation.

If you need to delete a connection, navigate to the Data section of the Builder and locate the connection you would like to delete from the table's fields. On the connection field, click the "..." more options icon, then click "Delete". This will remove the connection field from the app.

You can also open the field options and select the "Delete field" option while viewing the records in the Records section of the Builder:

This is the danger zone, connections are a fundamental part of the app structure. This means that records will no longer be linked together, and if pages or views are using these connections they may no longer work properly.

Use Connections in Your App

There are so many super powerful ways to use connections in your app. So many in fact, that this information needs its own article: Use Connections in Your App.

Notes & Troubleshooting

Notes

  • Only one connection field is needed to link two tables. However, there are rare cases when using two connection fields between two tables is needed. 
  • When adding connection fields, it is best to place the connection field on the "many" table. This means for one-to-many connections, place the connection on the table which will have many records connected. For many-to-many connections, place the connection field on whichever table is used most often.
  • If you have a connection field but connected records are not showing, double check the connection to make sure there is a value in the field. You can read more about troubleshooting connections here.

Troubleshooting

Warning: Edit or Delete a Connection

It is important to note that editing or deleting a connection can damage your app. Editing a connection field, may corrupt views in the Live App that use the connection. Deleting a connection field will destroy how records are connected across tables and corrupt any views using that connection in the Live App. 

Why is My Connection Not Working?

If you are having trouble seeing connected records or choosing connected records from the dropdown, first start by checking the connection field in the table. In the Data section of the Builder, select the table the connection field is on and view that table's records. There, check that the connection field is not blank. While the connection field links the two tables, the field needs to have a selected value in the connection field to link individual records.

You can read more about troubleshooting your connections here.

How to Guides

Once your tables are connected, you can use these connections in powerful ways throughout your application. 

How did we do?

Working With Tables

Plan Your Connections: Simple Example

Contact