The original version of this document is located at https://docs.qfield.org/how-to/project-setup/relation-reference-widget
Sometimes it can be useful to relate different layers with each other when they are depending on each other or when you want to add more than one record to a feature.
Example
In a building there are several apartments with different owners.
We can create a relation between the building and the apartments and between the apartments and the owners.
In such a case we make use of the relation reference widget to be able to add new children or to select a child from the existing ones.
Before adding, editing or viewing the related features you have to set up a relation between the two layers.
Here it is important that you add an id (primary key) field to the parent layer (Reference Layer) that can be used as a foreign key in the child layer (Referencing layer).
These fields are used for creating the link between the two layers.
Therefore, they must be unique.
Note: It is good practice to use uuid's as “unique ids” given they are 36 characters long and also contain non-numerical characters making them much safer to use over ordinary numerical ids.
Workflow
Creating the relation
Attribute Form Configuration - Reference Layer
reference field, set the widget type to UIID generator and set a default value to *uuid(‘WithoutBraces’)
Attribute Form Configuration - Referencing Layer
It is possible to limit the number of available children for your related layer if you are not interested in all items.
Workflow


In the case of many-to-many relations you will need a linking table, which commonly is also termed as a “pivot table”.
In the official QGIS documentation you will find a detailed description on how to establish these more complex relations.
If required you can reorder linked child features based on a field by selecting the Ordered Relation Editor from the widget type options.
To enable this functionality, however, you require a second plugin Ordered Relation Editor
Workflow
Install the Plugin Ordered Relation Editor plugin from the official repository or through the “Plugin Manager” in QGIS.
Open the Vector Layer Properties… > Attributes Form and set the layout editor to Drag and Drop Designer.
Click on the relationship of your available widgets.
Direct to Properties > Attribute Form and find your relation under the relationship section.
On the right under “Widget Display” scroll down to the “Widget Type option and select Ordered Relation Editor.
Configure the widget using the following settings:
Ordering Field: Specify the field in the child layer that will be used to determine the order of the features.
Description: Define an expression to be displayed for each child feature in the list.
Image Path (Optional): Provide a path to an image or icon to visually enhance the list. This is an expression that resolves dynamically.


It is possible to search values in a value relation or relation reference widget using the magnifying glass next to the field.

Note
Define the display expression for the concerned layers, this will be used for searching for features.
This is configured under Vector Layer Properties… > Display