Table Relationships¶
As your database grows, you may want to classify your features more efficiently. Instead of typing values manually (e.g., city, town, village) each time you add a new place, you can create a lookup table and link it to your existing places table.
This improves data consistency, prevents typing mistakes, and ensures your classifications follow a standard list.
Step 1: Create a Lookup Table¶
You’ll start by creating a new table called place_types.
-
In pgAdmin, navigate to kartoza_postgis → Schemas → public → Tables.
-
Right-click Tables, and select Create → Table.
-
In the General tab, fill in:
Property Value Name place_types Owner kartoza_postgis Schema public Tablespace pg_default -
Open the Columns tab and add:
Name Data Type Length Not Null? Primary Key? id integer — x x label character 128 x -
Click Save.
Step 2: Add Records¶
Now add some categories to classify your places.
-
Right-click the place_types table.
-
Select View/Edit Data → All Rows.
-
Click the Add Row button and add values such as:
id label 1 City 2 Town 3 Village 4 Settlement -
Click Save Data Changes.
Your lookup table is now populated.
Step 3: Add a Foreign Key Field¶
The places table needs a field that links each place to a type.
-
Click the Query Tool from the top toolbar.
-
Run the following command into the SQL Editor:
This new field will store the ID from place_types.
Step 4: Create the Relationship¶
Now enforce the relationship in the database so that place_type_id must match a value in your lookup table.
-
Run this SQL in Query Tool:
ALTER TABLE places ADD CONSTRAINT fk_place_type FOREIGN KEY (place_type_id) REFERENCES place_types (id);
Your tables are now formally linked in PostGIS.
Step 5: Configure in QGIS¶
Now that your lookup table is in place, you can connect it to your main layer.
-
Go back to QGIS.
-
Right-click your PostGIS connection → Refresh.
-
Load the new place_types table.
-
Open the Layer Properties for the places layer.
-
Go to the Attribute Form tab and select the place_type_id field.
-
Change its Widget Type to Value Relation.
-
In the widget settings:
Setting Value Layer place_types Key id Value label Allow Null Optional ✓ -
Click OK.
Now, whenever you add or edit a place, QGIS will show a dropdown list of place types instead of a free-text field.
Step 6: Test the Relationship¶
With the widget configured, you can now test how the relationship behaves during real editing.
-
Select the places layer.
-
Click the Toggle Editing button.
-
Select the Add Point Feature tool.
-
Click on the map at the location where you’d like to place your new feature.
-
On the Attribute Form you should now see a dropdown menu for place_type_id.
-
Fill in the relevant information, then click OK.
-
Click Save Layer Edits from the top toolbar.
You can now return to pgAdmin to verify the updated field.
Your places data is now linked to a clean, controlled classification system, an essential step toward building well-structured, professional spatial databases.
Next up: Learn the basics of SQL & Spatial Queries so you can filter, analyse, and explore your PostGIS data.