Today I stumbled across something I wouldn’t exactly consider a bug, but at least some rather unintuitive and annoying behavior in QGIS when performing table joins.
I did something very mundane: joining a Postgres table of spatial data to another Postgres table of attribute data. The normal way to do this (for me) is as follows:
- Open the spatial table using
Layer > Add Layer > Add PostGIS Layers...
- Open the attribute table the same way (1 & 2 can be loaded in one go)
- Join the tables in the spatial table’s
For that last step I decided to join the two tables (
plr is the spatial table here, while
mss has the attributes) using the field
plr_id, which exists in both tables and only once on each side (hence a plain vanilla 1:1 join).
That works perfectly fine, except that somehow the order of the joined fields appears to get messed up:
Some research revealed that this seems to be a problem caused by identical field names in the two joined tables other than the join field itself. In my case the aforementioned
plr_id was used to join the two tables, but in addition both tables also had a field
gid, as can be seen in the following screenshot on the left:
Removing this field
gid from the attribute table
mss was no problem, since the 1:1 relation to the spatial data uses the key
plr_id anyways. As can be seen in the screenshot above on the right, the new table
mss2 is identical to
mss, only without the field
gid. And lo-and-behold – joining this attribute table to the spatial table
plr in QGIS works flawlessly now:
This problem had already been identified in QGIS 2.0 in late 2013, and has been marked as fixed in the meantime. Removing fields with identical names in the two tables is one – admittedly rather radical way – to
solve circumvent the issue. Another, more intuitive way would be to choose a meaningful table prefix in the
Add vector join dialog which can be seen in the first image above. As you can see I checked the
Custom field name prefix checkbox but left the field empty. I prefer this, since it keeps my field names nice and tidy, but in cases where homonymous fields exist in the two tables you will run into trouble – hence entering a prefix here would be a nice and easy fix for this issue.
Everything described above was performed on
QGIS 2.8.1-Wien (64bit) on a Windows 7 machine and
PostgreSQL 9.1.16 on a
64bit Ubuntu 4.6.3 server (
Interesting, by chance I stumbled upon your blog. Roughly around the same time I found this unfortunate bug as well, in my case I’ve attempted to performed multiple spatial joins through the Graphical Modeler. I ended up getting a really big mess which was solved using a rather basic solution that of renaming the field. However, it took a bit of time to realize it.
My setup: QGIS 2.8.2 Wien, on a 64 bit Win7.
Ya, took me a while as well. I wouldn’t really consider it a bug, though… Unfortunately I still haven’t come around to enter it to the QGIS issue tracker, though. I guess an easy workaround would be for the join method to look for homonymous field names introduced by joins and add counters automatically (e.g. a.field JOIN b.field becomes a.field, a.field1 or something along these lines…). But knowing about this behavior is a good way to work around it – that’s why I decided to put it up here.
I logged this issue as a feature request with the QGIS issue tracker now: https://hub.qgis.org/issues/13036