You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/layer/relations/index.md
+38-42Lines changed: 38 additions & 42 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,17 +6,15 @@ outline: 2,3
6
6
---
7
7
8
8
# Relations
9
-
[[toc]]
10
9
11
10
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.
12
11
13
12
<QGISHelpver="latest"link="/user_manual/working_with_vector/joins_relations.html"text="See QGIS Documentation" /> to learn more about connecting and editing data across layers.
14
13
15
14
|<divstyle="width:150px"> Widget </div> |Preview in the <MobileAppNameShort />|
16
15
|:---:|:---:|
17
-
|[Relation](../one-to-n-relations/)||
18
-
|[Relation - Gallery ](../photos/#how-to-attach-multiple-photos-to-one-feature)||
19
-
|[Relation reference](#referencing-child-layer-attributes-form-relation-reference-widget)||
16
+
| View from parent (relation) ||
17
+
| View from child (relation reference) ||
20
18
21
19
:::tip Example projects and guides available
22
20
You can take a closer look at 1-N relations:
@@ -25,21 +23,25 @@ You can take a closer look at 1-N relations:
25
23
-[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: <MerginMapsProjectid="documentation/forms_multiple_photos" />
26
24
:::
27
25
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:
29
27
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
+

29
+
30
+
As an example, we will use a project <MerginMapsProjectid="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.
31
33
32
34
*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.
33
35
34
36
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.
35
37
36
-
## One to many relation setup
38
+
## One to many relation setup overview
37
39
One to many (1-N) relation needs to be set up in your <MainPlatformName /> project in QGIS as follows:
38
40
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
39
41
3. Define the relation in [Project Properties](#setting-up-relation-in-qgis)
40
42
4.[Configure Attributes forms](#attributes-forms-configuration-for-1-n-relations) of both layers as needed
41
43
42
-
###Generating unique IDs (UUID)
44
+
## Generating unique IDs (UUID)
43
45
44
46
:::tip UUID as unique primary keys
45
47
<QGISHelpver="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)`.
64
66
:::
65
67
66
68
67
-
###Setting up relation in QGIS
69
+
## Setting up relation in QGIS
68
70
Now let's look at the 1-N relation setup in QGIS:
69
71
1. From the main menu, select **Projects** > **Properties ...**
70
72

@@ -86,14 +88,14 @@ Now let's look at the 1-N relation setup in QGIS:
86
88
87
89
The one to many relation between **parks** and **amenities** is now set in QGIS. Next we can configure the forms for both layers.
88
90
89
-
###Attributes forms configuration for 1-N relations
91
+
## Attributes forms configuration for 1-N relations
90
92
Attributes form for both linked layers can be set up as needed, depending on the preferred data collection method for the referencing (child) layer:
91
93
- 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**.
92
94
- 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.
93
95
94
96
Both options work both in QGIS and in the [<MobileAppNameShort />](#relation-widgets-in-mergin-maps-mobile-app).
95
97
96
-
####Referenced (parent) layer attributes form - relations widget
98
+
### Referenced (parent) layer attributes form - relations widget
97
99
Explore the attributes form of the referenced (parent) layer **parks**:
98
100
1. Navigate to the **Attributes** form tab in **Layer Properties**
99
101
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:
108
110
109
111
Note the editable `park-amenities-relation` with the option to add new (child) features to the **amenities** layer directly from the **parks** form.
110
112
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
+

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
+

111
126
112
-
####Referencing (child) layer attributes form - relation reference widget
127
+
### Referencing (child) layer attributes form - relation reference widget
113
128
The referencing (child) layer **amenities** can also have the relation added to the attributes form.
114
129
115
130
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
127
142
128
143

129
144
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) <MerginMapsProjectid="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
-

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
-

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
-

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:
160
146
- Tap the **Add** button and capture the geometry
161
147
- 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.
162
148
- Save the changes.
163
149
164
150

165
151
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
+

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).
0 commit comments