Skip to content

Commit 572e9d0

Browse files
committed
relations
1 parent b7fda50 commit 572e9d0

File tree

3 files changed

+38
-42
lines changed

3 files changed

+38
-42
lines changed

src/layer/relations/index.md

Lines changed: 38 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,15 @@ outline: 2,3
66
---
77

88
# Relations
9-
[[toc]]
109

1110
There are cases when multiple records from one layer (or table) relate to one feature from another layer, forming one to many (1-N) relation. For instance, there may be multiple photos of one feature, multiple inspections of the same feature or multiple parts, or one feature consisting of multiple polygons with different properties. As these use cases vary, there are different options for how to set up forms for relations.
1211

1312
<QGISHelp ver="latest" link="/user_manual/working_with_vector/joins_relations.html" text="See QGIS Documentation" /> to learn more about connecting and editing data across layers.
1413

1514
|<div style="width:150px"> Widget </div> |Preview in the <MobileAppNameShort />|
1615
|:---:|:---:|
17-
| [Relation](../one-to-n-relations/) | ![Mergin Maps mobile app relation form](../form-widgets/mobile-form-relations-preview.jpg "Mergin Maps mobile app relation form") |
18-
|[Relation - Gallery ](../photos/#how-to-attach-multiple-photos-to-one-feature) | ![Mergin Maps mobile app multiple photos attributes form](../form-widgets/mobile-form-multiple-photo-preview.jpg "Mergin Maps mobile app multiple photos attributes form") |
19-
| [Relation reference](#referencing-child-layer-attributes-form-relation-reference-widget) | ![Mergin Maps mobile app relation reference form](./mobile-form-relation-reference-preview.webp "Mergin Maps mobile app relation reference form") |
16+
| View from parent (relation) | ![Mergin Maps mobile app relation form](./mobile-form-relation-preview.webp "Mergin Maps mobile app relation form") |
17+
| View from child (relation reference) | ![Mergin Maps mobile app relation reference form](./mobile-form-relation-reference-preview.webp "Mergin Maps mobile app relation reference form") |
2018

2119
:::tip Example projects and guides available
2220
You can take a closer look at 1-N relations:
@@ -25,21 +23,25 @@ You can take a closer look at 1-N relations:
2523
- [How to Attach Multiple Photos to Features ](../photos/#how-to-attach-multiple-photos-to-one-feature) shows how to add multiple **photos** to a single feature. See also our public project: <MerginMapsProject id="documentation/forms_multiple_photos" />
2624
:::
2725

28-
As an example, we will use a project with two spatial layers: **parks** and **amenities**. One park can have multiple amenities, such as benches, picnic tables, fireplaces or water fountains. Every amenity feature can be located in one park only. Thus, the relation between these layers is one to many (1-*N*): 1 park to *N* amenities.
26+
There are two ways to add features with relations in the <MobileAppNameShort />: using the [relation widget](#relation-widget) on the parent layer or using the [relation reference widget](#relation-reference-widget) on the child layer:
2927

30-
This means that **parks** are the referenced (parent) layer and **amenities** are the referencing (child) layer. They will be related based on the *primary keys* of parks and corresponding *foreign keys* of the amenities layer, linking amenities to a specific park.
28+
![Mergin Maps mobile app relation widgets](./mobile-relation-widgets.gif "Mergin Maps mobile app relation widgets")
29+
30+
As an example, we will use a project <MerginMapsProject id="documentation/forms-relations" /> with two spatial layers: **parks** and **amenities**. One park can have multiple amenities, such as benches, picnic tables, fireplaces or water fountains. Every amenity feature can be located in one park only. Thus, the relation between these layers is one to many (1-*N*): one park to many amenities.
31+
32+
This means that **parks** are the **referenced (parent) layer** and **amenities** are the **referencing (child) layer**. They will be related based on the *primary keys* of parks and corresponding *foreign keys* of the amenities layer, linking amenities to a specific park.
3133

3234
*Primary keys* are unique IDs. While all layers have a default ID field when created (`fid`, `id`), these IDs can be changed during synchronisation in <MainPlatformName />, e.g. when multiple surveyors capture new features at the same time (see [Behind data synchronisation](../../manage/synchronisation/#behind-data-synchronisation) for detailed explanation). As a result, child features could end up being linked to a wrong parent feature.
3335

3436
Therefore, **we strongly recommend creating a new field for primary keys and using UUIDs generated by the `uuid()` function when a feature is created [as described below](#generating-unique-ids-uuid)**. UUID (Universally Unique Identifier) ensures that the primary keys are unique even after synchronisation and that the correct features are linked.
3537

36-
## One to many relation setup
38+
## One to many relation setup overview
3739
One to many (1-N) relation needs to be set up in your <MainPlatformName /> project in QGIS as follows:
3840
1. [Create unique UUID field](#generating-unique-ids-uuid) for your referenced (parent) layer and a field for storing foreign keys in the referencing (child) layer
3941
3. Define the relation in [Project Properties](#setting-up-relation-in-qgis)
4042
4. [Configure Attributes forms](#attributes-forms-configuration-for-1-n-relations) of both layers as needed
4143

42-
### Generating unique IDs (UUID)
44+
## Generating unique IDs (UUID)
4345

4446
:::tip UUID as unique primary keys
4547
<QGISHelp ver="latest" link="user_manual/expressions/functions_list.html#uuid" text="UUID" /> (Universally Unique Identifier) is generated to be unique and will not be changed when synced. Therefore, **we strongly recommend using UUID to link layers**.
@@ -64,7 +66,7 @@ This field needs to have the same data type as the primary key: `Text (string)`.
6466
:::
6567

6668

67-
### Setting up relation in QGIS
69+
## Setting up relation in QGIS
6870
Now let's look at the 1-N relation setup in QGIS:
6971
1. From the main menu, select **Projects** > **Properties ...**
7072
![QGIS menu Project properties](./qgis-project-properties.webp "QGIS menu Project properties")
@@ -86,14 +88,14 @@ Now let's look at the 1-N relation setup in QGIS:
8688

8789
The one to many relation between **parks** and **amenities** is now set in QGIS. Next we can configure the forms for both layers.
8890

89-
### Attributes forms configuration for 1-N relations
91+
## Attributes forms configuration for 1-N relations
9092
Attributes form for both linked layers can be set up as needed, depending on the preferred data collection method for the referencing (child) layer:
9193
- Child features can be added through the form of the [referenced (parent) layer](#referenced-parent-layer-attributes-form-relations-widget), meaning you can use the attributes form of the **parks** layer to add/edit/remove **amenities**.
9294
- Child features can be added through the form of the [referencing (child) layer](#referencing-child-layer-attributes-form-relation-reference-widget). A new feature can be added to the **amenities** layer and the link to the corresponding **park** feature is set manually in the attributes form.
9395

9496
Both options work both in QGIS and in the [<MobileAppNameShort />](#relation-widgets-in-mergin-maps-mobile-app).
9597

96-
#### Referenced (parent) layer attributes form - relations widget
98+
### Referenced (parent) layer attributes form - relations widget
9799
Explore the attributes form of the referenced (parent) layer **parks**:
98100
1. Navigate to the **Attributes** form tab in **Layer Properties**
99101
2. Notice the `park-amenities-relation` was automatically added to the list of available widgets as **Relations**.
@@ -108,8 +110,21 @@ In QGIS, the attributes form looks like this:
108110

109111
Note the editable `park-amenities-relation` with the option to add new (child) features to the **amenities** layer directly from the **parks** form.
110112

113+
Now look at this form in <MobileAppName/>. The referenced (parent) features have the [relation widget](#referenced-parent-layer-attributes-form-relations-widget) in the attributes form. It displays all linked child features in a row.
114+
115+
Here, **parks** feature `Park B` already contains a link to a `bench` from the **amenities** layer. Tapping on the child feature `bench` opens its attributes form with the option to edit the feature.
116+
117+
A new child feature can be added by tapping the **+** button:
118+
119+
![Mergin Maps mobile app relation widget](./mobile-form-relation-widget.webp "Mergin Maps mobile app relation widget")
120+
121+
- If the referencing (child) layer is a spatial layer, you will be asked to record geometry. Here **amenities** are captured as points. Note that it is not possible to change the [active layer](../../field/mobile-features/#adding-features).
122+
- In the form, you can fill in information about the child feature (here we add a `picnic table`). Note that the relation reference (foreign key `fk-uuid`) is filled in automatically based on the parent feature (here `Park B`).
123+
- Save the changes. The form of the `Park B` feature now displays one more entry in the relation widget (`picnic table`).
124+
125+
![Mergin Maps mobile app relation widget added child feature](./mobile-form-added-child-feature.webp "Mergin Maps mobile app relation widget added child feature")
111126

112-
#### Referencing (child) layer attributes form - relation reference widget
127+
### Referencing (child) layer attributes form - relation reference widget
113128
The referencing (child) layer **amenities** can also have the relation added to the attributes form.
114129

115130
1. Navigate to the **Attributes** form tab in **Layer Properties**
@@ -127,39 +142,20 @@ In this case, the link between the amenity feature and the park can be set (or e
127142

128143
![QGIS attributes form relation reference](./qgis-form-child.webp "QGIS attributes form relation reference")
129144

130-
131-
## Relation widgets in Mergin Maps mobile app
132-
Now let's look how these relation widgets look like in the <MobileAppNameShort />.
133-
134-
:::tip Example project available
135-
[Clone the public project](../../manage/create-project/#clone-a-public-project) <MerginMapsProject id="documentation/forms-relations" /> to see the setup and sample data on your mobile device.
136-
:::
137-
138-
There are two ways to add child features to a parent feature in the <MobileAppNameShort />: using the [relation widget](#relation-widget) on the parent layer or using the [relation reference widget](#relation-reference-widget) on the child layer.
139-
140-
![Mergin Maps mobile app relation widgets](./mobile-relation-widgets.gif "Mergin Maps mobile app relation widgets")
141-
142-
### Relation widget
143-
144-
The referenced (parent) features have the [relation widget](#referenced-parent-layer-attributes-form-relations-widget) in the attributes form. It displays all linked child features in a row.
145-
146-
Here, **parks** feature `Park B` already contains a link to a `bench` from the **amenities** layer. Tapping on the child feature `bench` opens its attributes form with the option to edit the feature.
147-
148-
A new child feature can be added by tapping the **+** button:
149-
150-
![Mergin Maps mobile app relation widget](./mobile-form-relation-widget.webp "Mergin Maps mobile app relation widget")
151-
152-
- If the referencing (child) layer is a spatial layer, you will be asked to record geometry. Here **amenities** are captured as points. Note that it is not possible to change the [active layer](../../field/mobile-features/#adding-features).
153-
- In the form, you can fill in information about the child feature (here we add a `picnic table`). Note that the relation reference (foreign key `fk-uuid`) is filled in automatically based on the parent feature (here `Park B`).
154-
- Save the changes. The form of the `Park B` feature now displays one more entry in the relation widget (`picnic table`).
155-
156-
![Mergin Maps mobile app relation widget added child feature](./mobile-form-added-child-feature.webp "Mergin Maps mobile app relation widget added child feature")
157-
158-
### Relation reference widget
159-
You can also add a new feature to the referencing (child) layer directly:
145+
It works the same in the <MobileAppName/>. You can add a new feature to the referencing (child) layer:
160146
- Tap the **Add** button and capture the geometry
161147
- Fill in the Attributes form. Note that the [relation reference](#referencing-child-layer-attributes-form-relation-reference-widget) (foreign key, here `fk-uuid`) is empty and needs to be entered manually by choosing a feature from the referenced (parent) layer.
162148
- Save the changes.
163149

164150
![Mergin Maps mobile app relation widget added child feature](./mobile-add-child-feature.webp "Mergin Maps mobile app relation widget added child feature")
165151

152+
## Gallery?
153+
Did you follow these steps and do you see something like this? If the referencing (child) layer is set up with the [Attachment widget](../photos) for photos, the form in the <MobileAppNameShort/> will automatically display a gallery.
154+
155+
![Many photos to a single feature in Mergin Maps mobile app](../photos/mobile-multiple-photos.jpg "Many photos to a single feature in Mergin Maps mobile app")
156+
157+
::: tip
158+
To display the [relations widget](#referenced-parent-layer-attributes-form-relations-widget) without the gallery, use `nogallery` in the relation name or label.
159+
:::
160+
161+
Recommended setup for gallery is explained in [Attaching multiple photos to one feature ](#attaching-multiple-photos-to-one-feature).
17.7 KB
Loading
66.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)