Skip to content
Permalink
Browse files

Merge pull request #36831 from elpaso/bugfix-36205-db-manager-duplica…

…ted-fields-queries

Fixes DB manager queries with duplicated field names
  • Loading branch information
elpaso committed Jun 2, 2020
2 parents d4a5d9c + e84ad91 commit e0d7d67186adbb1bfb42405f9df6a8b81bc1a98c
Showing with 18 additions and 1 deletion.
  1. +5 −1 src/providers/postgres/qgspostgresprovider.cpp
  2. +13 −0 tests/src/python/test_provider_postgres.py
@@ -1149,7 +1149,11 @@ bool QgsPostgresProvider::loadFields()
if ( fields.contains( fieldName ) )
{
QgsMessageLog::logMessage( tr( "Duplicate field %1 found\n" ).arg( fieldName ), tr( "PostGIS" ) );
return false;
// In case of read-only query layers we can safely ignore the issue
if ( ! mIsQuery )
{
return false;
}
}

fields << fieldName;
@@ -2387,6 +2387,19 @@ def testAddFeature(self):
self.assertFalse(l.dataProvider().addFeatures([f1, f2]),
'Provider reported no AddFeatures capability, but returned true to addFeatures')

def testDuplicatedFieldNamesInQueryLayers(self):
"""Test regresssion GH #36205"""

vl = QgsVectorLayer(self.dbconn + ' sslmode=disable key=\'__rid__\' table="(SELECT row_number() OVER () AS __rid__, * FROM (SELECT * from qgis_test.some_poly_data a, qgis_test.some_poly_data b where ST_Intersects(a.geom,b.geom)) as foo)" sql=', 'test_36205', 'postgres')
self.assertTrue(vl.isValid())
self.assertEqual(vl.featureCount(), 3)

# This fails because the "geom" field and "pk" fields are ambiguous
# There is no easy fix: all duplicated fields should be explicitly aliased
# and the query internally rewritten
# feature = next(vl.getFeatures())
# self.assertTrue(vl.isValid())


if __name__ == '__main__':
unittest.main()

0 comments on commit e0d7d67

Please sign in to comment.
You can’t perform that action at this time.