Skip to content

Commit

Permalink
Merge pull request #57817 from qgis/backport-57653-to-queued_ltr_back…
Browse files Browse the repository at this point in the history
…ports

[Backport queued_ltr_backports] Fix compound key discovery
  • Loading branch information
strk authored Jun 21, 2024
2 parents 2ab628a + 6afa36a commit 73bdae8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
11 changes: 4 additions & 7 deletions src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5184,8 +5184,7 @@ QList<QgsRelation> QgsPostgresProvider::discoverRelations( const QgsVectorLayer
" a.attname as column_name, "
" fk.constraint_schema, "
" referenced_table as table_name, "
" af.attname as column_name, "
" fk.confkey as ordinal_position "
" af.attname as column_name "
"FROM foreign_keys fk "
"JOIN pg_attribute af ON af.attnum = fk.confkey "
"AND af.attrelid = fk.confrelid "
Expand Down Expand Up @@ -5219,13 +5218,11 @@ QList<QgsRelation> QgsPostgresProvider::discoverRelations( const QgsVectorLayer
refSchema = refSchema.mid( 1, refSchema.length() - 2 );
}
const QString refColumn = sqlResult.PQgetvalue( row, 4 );
const QString position = sqlResult.PQgetvalue( row, 5 );
// try to find if we have layers for the referenced table
const QList<QgsVectorLayer *> foundLayers = searchLayers( layers, mUri.connectionInfo( false ), refSchema, refTable );
if ( ( position == QLatin1String( "1" ) ) || ( !refTableFound.contains( refTable ) ) )
if ( !refTableFound.contains( refTable ) )
{
// first reference field => try to find if we have layers for the referenced table
const auto constFoundLayers = foundLayers;
for ( const QgsVectorLayer *foundLayer : constFoundLayers )
for ( const QgsVectorLayer *foundLayer : foundLayers )
{
QgsRelation relation;
relation.setName( name );
Expand Down
10 changes: 10 additions & 0 deletions tests/src/python/test_provider_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -2511,6 +2511,16 @@ def testValidLayerDiscoverRelations(self):
vl
]

for lyr in vls:
self.assertTrue(lyr.isValid())
QgsProject.instance().addMapLayer(lyr)
relations = vl.dataProvider().discoverRelations(vl, vls)
self.assertEqual(len(relations), 2)
for i, r in enumerate(relations):
self.assertEqual(r.referencedLayer(), vls[i])
self.assertEqual(len(relations[0].fieldPairs()), 1)
self.assertEqual(len(relations[1].fieldPairs()), 1)

def testValidLayerDiscoverRelationsComposite(self):
"""
Test implicit relations that can be discovered between tables, based on declared composite foreign keys.
Expand Down
4 changes: 3 additions & 1 deletion tests/testdata/provider/testdata_pg.sql
Original file line number Diff line number Diff line change
Expand Up @@ -787,5 +787,7 @@ CREATE TABLE qgis_test.referencing_layer_composite(
fk_ref_4 integer,
CONSTRAINT fk_ref_3_4
FOREIGN KEY (fk_ref_3, fk_ref_4)
REFERENCES qgis_test.referenced_layer_composite(pk_ref_3, pk_ref_4)
-- NOTE: referenced cols are given in reverse order to guard
-- against issue GH-56420
REFERENCES qgis_test.referenced_layer_composite(pk_ref_4, pk_ref_3)
);

0 comments on commit 73bdae8

Please sign in to comment.