Skip to content

Commit c225f56

Browse files
committed
[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.
1 parent 795c27f commit c225f56

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/providers/postgres/qgspgtablemodel.cpp

+17-3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ void QgsPgTableModel::addTableEntry( const QgsPostgresLayerProperty &layerProper
5656
}
5757

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

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

106+
if ( !layerProperty.pkCols.isEmpty() )
107+
{
108+
// If we have a view with multiple possible columns to be used as the primary key, for convenience
109+
// let's select the first one - this is what the browser dock already does. We risk that a wrong column
110+
// will be used, but most of the time we should be fine.
111+
QString firstCol = layerProperty.pkCols[0];
112+
pkItem->setText( firstCol );
113+
pkItem->setData( QStringList( firstCol ), Qt::UserRole + 2 );
114+
}
115+
104116
QStandardItem *selItem = new QStandardItem( QString() );
105117
selItem->setFlags( selItem->flags() | Qt::ItemIsUserCheckable );
106118
selItem->setCheckState( Qt::Checked );
@@ -123,15 +135,17 @@ void QgsPgTableModel::addTableEntry( const QgsPostgresLayerProperty &layerProper
123135

124136
Q_FOREACH ( QStandardItem *item, childItemList )
125137
{
138+
if ( tip.isEmpty() || withTipButSelectable )
139+
item->setFlags( item->flags() | Qt::ItemIsSelectable );
140+
else
141+
item->setFlags( item->flags() & ~Qt::ItemIsSelectable );
142+
126143
if ( tip.isEmpty() )
127144
{
128-
item->setFlags( item->flags() | Qt::ItemIsSelectable );
129145
item->setToolTip( QString() );
130146
}
131147
else
132148
{
133-
item->setFlags( item->flags() & ~Qt::ItemIsSelectable );
134-
135149
if ( item == schemaNameItem )
136150
item->setData( QgsApplication::getThemeIcon( QStringLiteral( "/mIconWarning.svg" ) ), Qt::DecorationRole );
137151

0 commit comments

Comments
 (0)