# How to Link Multiple Records to One Feature (1-N Relations)

TIP

You can clone these projects to take a closer look on 1-N relations:

It is often the case that you have a set of spatial features and you want to record their status every now and then. For example, there is a GIS layer representing the manholes and the surveyors carry out regular inspections of the manholes using Mergin Maps. Instead of duplicating the manhole layer and recording each inspection as a new feature, inspections can be recorded in a non-spatial table that is linked to the spatial layer. This way, multiple records can be linked to one feature.

The image below shows the manhole locations and a form with listed inspections in Mergin Maps mobile app.

Multiple inspections linked to one point in Mergin Maps mobile app

The manhole point layer has the following attribute table:

fid Manhole Manhole UUID
1 1 {70c59616-492e-4757-aa9a-ee61b207ce94}
2 2 {be01b98f-3585-49d4-be74-4cf3530a2989}
3 3 {03178264-0070-45c8-a981-b2474627d7e0}

This layer contains only information about the manholes. Manhole UUID values are generated using uuid() function as a default value when a feature is created. This ensures that these values are unique even when multiple surveyors capture new features at the same time. This field will be used to link inspections and manholes.

WARNING

Why UUID? FID can be changed during synchronisation. As a result, records can end up being linked to wrong features.

On the other hand, QGIS Documentation UUID (opens new window) (Universally Unique Identifier) is generated to be unique and will not be changed when synced. Therefore, we recommend always using UUID to link layers.

Inspections are recorded in a separate non-spatial table with attribute table such as:

Inspection Date Blocked? Flooded? Inspector Manhole UUID
10/05/2022 Joe Schmoe {70c59616-492e-4757-aa9a-ee61b207ce94}
10/05/2022 ✔️ John Doe {03178264-0070-45c8-a981-b2474627d7e0}
12/05/2022 ✔️ Fred Bloggs {70c59616-492e-4757-aa9a-ee61b207ce94}
14/05/2022 ✔️ ✔️ Joe Schmoe {be01b98f-3585-49d4-be74-4cf3530a2989}

In this table, all information about the inspections are recorded. Manhole UUID is filled in automatically based on a 1-N relation that we will set up in QGIS.

The same principle can be used when you want to capture multiple photos for a single feature.

# How to set up 1-N relations in QGIS project

You can follow this example by cloning Mergin Maps Project documentation/forms_one-to-many-relations documentation/forms_one-to-many-relations (opens new window).

TIP

Make sure that your survey layer has a unique UUID field to create the link correctly. You will find detailed steps how to set it up in How to Attach Multiple Photos to Features

To configure 1-N relations in QGIS:

  1. From the main menu, select Projects > Properties ...

  2. In the Relations tab, select Add Relation
    Layer properties in QGIS with highlighted Relations tab and Add Relation button

  3. A new window will appear, where we can define the parent and child layers and the fields to link the two layers:

    • Name is the name of the relation, e.g. Inspection
    • Referenced (parent) is the spatial layer manhole_locations
    • Field 1 of the Referenced (parent) is the field Manhole UUID that contains the unique UUID
    • Referencing (child) is the non-spatial layer inspections
    • Field 1 of the Referencing (child) layer is the Manhole UUID, which acts as a foreign key to link inspections to spatial features

    Add Relation form in QGIS with defined 1-N relation between the parent and the child layer

  4. Right-click on the survey layer, select Properties and go to the Attributes form tab.

  5. Drag and drop the Inspections relation to the Form Layout.
    Relation added to the Drag and Drop Form Layout in QGIS

Now you can add multiple inspections for each manhole location. The inspections records will be stored in the inspections table.

When you open the form for an existing record in the manhole_locations point layer, it will display existing inspection records and you can also add, delete or edit the records:
Form view of a feature with 1-N relation in QGIS

In Mergin Maps mobile app, the form will look like this:

Form view of a feature with 1-N relation in Mergin Maps mobile app