[GIS] QGIS and 1-to-many relationship

attribute-joinsone-to-manyqgisvirtual-layer

I am using QGIS 3.10.0

I am attempting to join a spatial vector layer to a non-spatial layer (CSV).

Each polygon on the spatial layer has a unique id attribute. The CSV also has this id attribute but it is not unique for each record. So for each polygon on the spatial layer the are 1-to-N associated records in the CSV.

I was hoping that when you select an object on the spatial layer it will show all the associated records from the CSV.

I initially used the layer join option but that only shows one record from the CSV.

I then read about virtual layers. I created a virtual layer of both existing layers and this does what I want… however this is so slow it is unusable! After creating the layer it takes 10 minutes for QGIS to become responsive. If I do anything with that layer (select an object or just zoom!) I have to wait another 5 to 10 minutes! This is on 64-bit machine core i7 16gb ram.

The spatial layer has 7988 records and the csv has 8341 records.

Does anyone have any other options or know how to speed the virtual layer rendering up?

Best Answer

For a 1-n relationship it's better to use a relation rather than a join (a join is good for 1-1 relation). A relation should be faster than virtual layer and doesn't require you to create a new layer that wont update with your data.

The parent/child relation is displayed in the attribute form and let you easily update both at the same time (in your case a CSV won't be editable, you will need to convert to another format if you need to edit from QGIS). See link at the bottom for more information on how it work

The setting for relation is not in layer properties but at the project level (in the project properties), the setting are similar for relation and join but for the "Relationship strength" setting (The default Association type means that the parent layer is simply linked to the child one while the Composition type allows you to duplicate also the child features when duplicating the parent ones)

enter image description here

More on relation : QGIS user manual

Related Question