QGIS Relations Usage – Using Relations in QGIS: A Detailed Guide

qgisrelation

I am using QGIS, version 3.24.0. I have a shapefile called "Map_CorrectionDEF", uniquely identify by fields "GID" and "unitcode". It is a grid cell, in which the field "GID" is the cell and "unitcode" the country to which the cell belongs.

On the other hand, I have a CSV file, called crop_main (no geometry), uniquely identify by "unitcode" and "cropclass", where "unitcode" refers to the country and "cropclass" the type of crop.

I present an example of my CSV file:

enter image description here

I want to connect both fields. I would like to assign to each cell the type of crop which exist in the country which the cell belongs. More specific, for each cell, which belongs to a country ("unitcode" field), I would like to join every value of my csv file with has the same value of "unitcode". The goal is to obtain for all the cells of my shape file the information of all cropland which grows in the country. Hence, all the cells of the same country should have the same number of crops.

To do it (I am not sure if the correct way), I am doing the following relations:

enter image description here

However, when I do it, I am not able to visualize the relation I am creating. According to this, https://docs.qgis.org/3.10/en/docs/user_manual/working_with_vector/attribute_table.html?#creating-one-or-many-to-many-relations "depending on your settings, the form might open directly or you will have to choose to open it in the identification dialog under actions." In my case it's not open directly, but I am not able to go to the identification dialog.

I add a screenshot of what I see when I click one my cells in the map after doing the relation which @J.R proposed in his answer.

enter image description here

Second edit: I attach an screenshot of how I try to match the 1:many with aggregate function according to 1n-relations-in-label-expressions-general-syntax

enter image description here

Best Answer

You just need to set the two layer (parent : "Map_CorrectionDEF", and child : "crop_main") then set the field that link them, in your case the country field: "unitcode" for both layer.

This way when you click in a cell you will see all entry from the CSV table that share the same unitcode (be aware that relation don't work like join, you wont get the CSV column added to your grid table but you will see a form with, on top the grid attribute and below an extract of the attribute table of related CSV entry. see image below for example from QGIS doc on relation)

enter image description here

If your goal is to get a field in your grid table with a list of all crop for the country you may try the solution from here