Skip to content

Commit

Permalink
PostgreSQL foreign tables: handle them like views (fixes #30990) [nee…
Browse files Browse the repository at this point in the history
…ds-docs]

PostgreSQL foreign tables are not explicitely supported by the PosgreSQL provider.
This commit removes the error message, and handle foreign tables like views.

A foreign table cannot (in PostgreSQL <10) state what column
is the primary key, so we let the user tell QGIS.
  • Loading branch information
Alban Kraus committed Jul 30, 2019
1 parent b68f5c8 commit da9f1ae
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions src/providers/postgres/qgspostgresconn.cpp
Expand Up @@ -543,6 +543,7 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
QString relkind = result.PQgetvalue( idx, 6 );
bool isView = relkind == QLatin1String( "v" ) || relkind == QLatin1String( "m" );
bool isMaterializedView = relkind == QLatin1String( "m" );
bool isForeignTable = relkind == QLatin1String( "f" );
QString comment = result.PQgetvalue( idx, 7 );

int srid = ssrid.isEmpty() ? std::numeric_limits<int>::min() : ssrid.toInt();
Expand Down Expand Up @@ -577,7 +578,7 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
layerProperty.isView = isView;
layerProperty.isMaterializedView = isMaterializedView;
layerProperty.tableComment = comment;
addColumnInfo( layerProperty, schemaName, tableName, isView );
addColumnInfo( layerProperty, schemaName, tableName, isView || isForeignTable );

if ( isView && layerProperty.pkCols.empty() )
{
Expand Down Expand Up @@ -665,6 +666,7 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
QString coltype = result.PQgetvalue( i, 4 ); // column type
bool isView = relkind == QLatin1String( "v" ) || relkind == QLatin1String( "m" );
bool isMaterializedView = relkind == QLatin1String( "m" );
bool isForeignTable = relkind == QLatin1String( "f" );
QString comment = result.PQgetvalue( i, 5 ); // table comment

//QgsDebugMsg( QStringLiteral( "%1.%2.%3: %4" ).arg( schemaName ).arg( tableName ).arg( column ).arg( relkind ) );
Expand Down Expand Up @@ -699,7 +701,7 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
Q_ASSERT( !"Unknown geometry type" );
}

addColumnInfo( layerProperty, schemaName, tableName, isView );
addColumnInfo( layerProperty, schemaName, tableName, isView || isForeignTable );
if ( isView && layerProperty.pkCols.empty() )
{
//QgsDebugMsg( QStringLiteral( "no key columns found." ) );
Expand Down Expand Up @@ -754,6 +756,7 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
QString relkind = result.PQgetvalue( i, 2 ); // relation kind
bool isView = relkind == QLatin1String( "v" ) || relkind == QLatin1String( "m" );
bool isMaterializedView = relkind == QLatin1String( "m" );
bool isForeignTable = relkind == QLatin1String( "f" );
QString comment = result.PQgetvalue( i, 3 ); // table comment

//QgsDebugMsg( QStringLiteral( "%1.%2: %3" ).arg( schema ).arg( table ).arg( relkind ) );
Expand Down Expand Up @@ -784,7 +787,7 @@ bool QgsPostgresConn::getTableInfo( bool searchGeometryColumnsOnly, bool searchP
if ( alreadyFound )
continue;

addColumnInfo( layerProperty, schema, table, isView );
addColumnInfo( layerProperty, schema, table, isView || isForeignTable );
layerProperty.sql.clear();

mLayersSupported << layerProperty;
Expand Down
2 changes: 1 addition & 1 deletion src/providers/postgres/qgspostgresprovider.cpp
Expand Up @@ -1420,7 +1420,7 @@ bool QgsPostgresProvider::determinePrimaryKey()
QgsMessageLog::logMessage( tr( "The table has no column suitable for use as a key. QGIS requires a primary key, a PostgreSQL oid column or a ctid for tables." ), tr( "PostGIS" ) );
}
}
else if ( type == Relkind::View || type == Relkind::MaterializedView )
else if ( type == Relkind::View || type == Relkind::MaterializedView || type == Relkind::ForeignTable )
{
determinePrimaryKeyFromUriKeyColumn();
}
Expand Down

0 comments on commit da9f1ae

Please sign in to comment.