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 appartments with different owners. We can create a relation between the building and the appartments and between the appartments 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.
Relation configuration¶
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
- Direct to Project > Properties > Relations
- Press the green plus to add a new relation
- Set your Reference and your Referenced Layer in addition to the corresponding fields that will be used for the linking. For more details please refer to the official QGIS documentation
Attribute Form Configuration - Reference Layer
- Direct to the Layer Properties > Attribute Form
- Find your
reference field
, set the widget type to UIID generator and set a default value to *uuid('WithoutBraces') - Find your relation under the "Relations Section" and add it to the form layout
- Set the Cardinality to "Many to one relation"
- Under the widget you can define whether children can be:
- linked
- unlinked
- edited
- added
- duplicated
- deleted
- zoomed into
- (Optional) If you want to filter your children further, you can use additional "expressions".
- Once finished click "ok"
Attribute Form Configuration - Referencing Layer
- Direct to the Layer Properties > Attribute Form
- Configure your attribute form with the wanted fields.
- (Optional): Under Display you can set the appearance of how the children will be displayed
Maximum number of visible children¶
It is possible to limit the number of available children for your related layer if you are not interested in all items.
- Default Number of visible children: 4 children
- Unlimited: Empty
Workflow
- Direct to Vector Layer Properties... > QField.
- Under "Relationship Settings" set the "Maximum number of items visible".
多対多のリレーション¶
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.
Ordered Relation¶
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