Skip to content

Commit

Permalink
Merge pull request #36831 from elpaso/bugfix-36205-db-manager-duplica…
Browse files Browse the repository at this point in the history
…ted-fields-queries

Fixes DB manager queries with duplicated field names
  • Loading branch information
elpaso committed Jun 2, 2020
2 parents d4a5d9c + e84ad91 commit e0d7d67
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions tests/src/python/test_provider_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.