Skip to content
Permalink
Browse files

[postgres] Fix addition of views from Data Source Manager (fixes #19424)

In order to add a PostgreSQL layer based on a view, one needs to explicitly
pick one or more columns to serve as the primary key. However in both browser
dock and in the DB manager user can add a view as a layer without specifying
primary key. Users get confused about this behavior, therefore this commit
makes the Data Source Manager behave consistently with browser and DB manager,
that is it will pick the first column as the proposed primary key automatically.

While this may be a bit risky in letting user use wrong pkey, it is very
convenient (and consistent with other part of QGIS). Also, usability of selection
of geometry type / srid / pkey column(s) is not great so it is good not to force
people to always choose pkey for their views. The list will still keep
the warning icon and tooltip shown as before.
  • Loading branch information
wonder-sk committed Oct 8, 2018
1 parent 795c27f commit c225f56026bf12c6068ad8a6b087e4194b3e8fc8
Showing with 17 additions and 3 deletions.
  1. +17 −3 src/providers/postgres/qgspgtablemodel.cpp
@@ -56,6 +56,7 @@ void QgsPgTableModel::addTableEntry( const QgsPostgresLayerProperty &layerProper
}

QString tip;
bool withTipButSelectable = false;
if ( wkbType == QgsWkbTypes::Unknown )
{
tip = tr( "Specify a geometry type in the '%1' column" ).arg( tr( "Data Type" ) );
@@ -67,6 +68,7 @@ void QgsPgTableModel::addTableEntry( const QgsPostgresLayerProperty &layerProper
else if ( !layerProperty.pkCols.isEmpty() )
{
tip = tr( "Select columns in the '%1' column that uniquely identify features of this layer" ).arg( tr( "Feature id" ) );
withTipButSelectable = true;
}

QStandardItem *schemaNameItem = new QStandardItem( layerProperty.schemaName );
@@ -101,6 +103,16 @@ void QgsPgTableModel::addTableEntry( const QgsPostgresLayerProperty &layerProper
pkItem->setData( layerProperty.pkCols, Qt::UserRole + 1 );
pkItem->setData( "", Qt::UserRole + 2 );

if ( !layerProperty.pkCols.isEmpty() )
{
// If we have a view with multiple possible columns to be used as the primary key, for convenience
// let's select the first one - this is what the browser dock already does. We risk that a wrong column
// will be used, but most of the time we should be fine.
QString firstCol = layerProperty.pkCols[0];
pkItem->setText( firstCol );
pkItem->setData( QStringList( firstCol ), Qt::UserRole + 2 );
}

QStandardItem *selItem = new QStandardItem( QString() );
selItem->setFlags( selItem->flags() | Qt::ItemIsUserCheckable );
selItem->setCheckState( Qt::Checked );
@@ -123,15 +135,17 @@ void QgsPgTableModel::addTableEntry( const QgsPostgresLayerProperty &layerProper

Q_FOREACH ( QStandardItem *item, childItemList )
{
if ( tip.isEmpty() || withTipButSelectable )
item->setFlags( item->flags() | Qt::ItemIsSelectable );
else
item->setFlags( item->flags() & ~Qt::ItemIsSelectable );

if ( tip.isEmpty() )
{
item->setFlags( item->flags() | Qt::ItemIsSelectable );
item->setToolTip( QString() );
}
else
{
item->setFlags( item->flags() & ~Qt::ItemIsSelectable );

if ( item == schemaNameItem )
item->setData( QgsApplication::getThemeIcon( QStringLiteral( "/mIconWarning.svg" ) ), Qt::DecorationRole );

0 comments on commit c225f56

Please sign in to comment.
You can’t perform that action at this time.