What is a Connection?
Most apps will need more than one object. When this is the case, these objects 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 object can be connected to records from another object. These connections can be used throughout your app to add powerful functionality.
To use connections, you will need to have at least two objects. If you want to learn more about how to use objects you can read more here.
In the contact directory example app, we have two objects: 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 object, where each note record is connected to a contact record. This enables us to show notes records connected to each contact record.
In the Data section of the Builder when viewing an object's fields, each object has a Connections section along the right side of the Builder. Here you can add new connections to the object, as well as view any existing connection fields.
Add a Connection
To add a connection field, select an object in the Data section of the Builder and view the "Fields" tab for that object. Once there, you can use the "Add connection" button in the right column to add a new connection field.
This will present a new window where you select the object you want the connection to connect to:
Once you have selected the object 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 objects 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 object. When you add a new connection, it creates a special connection field in that object:
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 object to connect to.
Which Object to Add Your Connection To
You only need to add one connection field to create a connection between two objects, so which object should you add that field to?
For one-to-one and many-to-many relationships, we suggest placing the connection field on whichever object 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 object with many connections. Or to put it another way, in a parent-child relationship, it works best to add the connection to the child object.
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 object. 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 object (or the child object). The connection field to the Job object 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 object (or the parent object). Instead of connecting to one record it would need to connect to many or all the Application records:
This can be a little slower and might take a while to download the full list of applications. In this example, the job is only connected to two records ("Jenny Martin" and "Rick West"), but if there were hundreds of applications, this could get cumbersome.
Another reason the child object 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 object's fields.
Next, select the settings icon on the field, this will open a popup with the connection field options.
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.
- 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.
- Form: Choose between connection values displaying in a searchable dropdown or as radio buttons in your Live App.
Warning: Editing a connection field can corrupt existing connections between objects 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 object's fields. On the connection field, click the trash can icon, then click "Delete Field". This will remove the connection field from the app.
Warning: 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
- Only one connection field is needed to link two objects. However, there are rare cases when using two connection fields between two objects is needed.
- When adding connection fields, it is best to place the connection field on the "many" object. This means for one-to-many connections, place the connection on the object which will have many records connected. For many-to-many connections, place the connection field on whichever field 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.
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 objects 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 object. In the Data section of the Builder, select the object the connection field is on and view that object's records. There, check that the connection field is not blank. While the connection field links the two objects, 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 objects are connected, you can use these connections in powerful ways throughout your application.