Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes DB manager queries with duplicated field names #36831

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()