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

Fix PostgreSQL compound key discovery #57653

Merged
merged 4 commits into from
Jun 21, 2024
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
11 changes: 4 additions & 7 deletions src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5230,8 +5230,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 @@ -5265,13 +5264,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 @@ -2510,6 +2510,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)
);
Loading