Skip to content

Commit

Permalink
[postgres] Fix addition of views from Data Source Manager (fixes #19424)
Browse files Browse the repository at this point in the history
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 9, 2018
1 parent 795c27f commit c225f56
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/providers/postgres/qgspgtablemodel.cpp
Expand Up @@ -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" ) );
Expand All @@ -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 );
Expand Down Expand Up @@ -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 );
Expand All @@ -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 );

Expand Down

0 comments on commit c225f56

Please sign in to comment.