Skip to content

Commit

Permalink
handle mixed relation, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
speillet authored and nyalldawson committed Jan 27, 2021
1 parent c75e160 commit 0847f1e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -5035,9 +5035,17 @@ QList<QgsRelation> QgsPostgresProvider::discoverRelations( const QgsVectorLayer
else
{
// multi reference field => add the field pair to all the referenced layers found
const QList<QgsVectorLayer *> foundLayers = searchLayers( layers, mUri.connectionInfo( false ), refSchema, refTable );
const auto constFoundLayers = foundLayers;
for ( int i = 0; i < nbFound; ++i )
{
result[result.size() - 1 - i].addFieldPair( fkColumn, refColumn );
for ( const QgsVectorLayer *foundLayer : constFoundLayers )
{
if ( result[result.size() - 1 - i].referencedLayerId() == foundLayer->id() )
{
result[result.size() - 1 - i].addFieldPair( fkColumn, refColumn );
}
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion tests/src/python/test_qgsrelationpostgres.py
Expand Up @@ -61,7 +61,9 @@ def test_discover_relations(self):
QgsProject.instance().addMapLayer(vl_tables[i])

relations = self.relMgr.discoverRelations([], vl_tables)
self.assertEqual(len(relations), 10)
for r in relations:
self.assertTrue(r.isValid())
self.assertEqual(len(relations), 18)
self.assertEqual(sum([len(r.referencingFields()) for r in relations]), 18)
self.assertEqual(sum([len(r.referencedFields()) for r in relations]), 18)

Expand Down

0 comments on commit 0847f1e

Please sign in to comment.