diff --git a/python/plugins/db_manager/db_plugins/vlayers/connector.py b/python/plugins/db_manager/db_plugins/vlayers/connector.py index c04503cd8f0..2182cb5ddc2 100644 --- a/python/plugins/db_manager/db_plugins/vlayers/connector.py +++ b/python/plugins/db_manager/db_plugins/vlayers/connector.py @@ -96,6 +96,9 @@ class VLayerRegistry(object): lid = self.layers.get(l) if lid is None: return lid + if lid not in QgsProject.instance().mapLayers().keys(): + self.layers.pop(l) + return None return QgsProject.instance().mapLayer(lid) @@ -249,12 +252,16 @@ class VLayerConnector(DBConnector): def getTableRowCount(self, table): t = table[1] l = VLayerRegistry.instance().getLayer(t) + if not l or not l.isValid(): + return None return l.featureCount() def getTableFields(self, table): """ return list of columns in table """ t = table[1] l = VLayerRegistry.instance().getLayer(t) + if not l or not l.isValid(): + return [] # id, name, type, nonnull, default, pk n = l.dataProvider().fields().size() f = [(i, f.name(), f.typeName(), False, None, False) @@ -280,6 +287,8 @@ class VLayerConnector(DBConnector): l = QgsProject.instance().mapLayer(t) else: l = VLayerRegistry.instance().getLayer(t) + if not l or not l.isValid(): + return None e = l.extent() r = (e.xMinimum(), e.yMinimum(), e.xMaximum(), e.yMaximum()) return r diff --git a/python/plugins/db_manager/layer_preview.py b/python/plugins/db_manager/layer_preview.py index ef50bb40f21..dbb35f20b77 100644 --- a/python/plugins/db_manager/layer_preview.py +++ b/python/plugins/db_manager/layer_preview.py @@ -113,15 +113,16 @@ class LayerPreview(QgsMapCanvas): else: vl = table.toMapLayer() - if not vl.isValid(): + if vl and not vl.isValid(): vl.deleteLater() vl = None # remove old layer (if any) and set new if self.currentLayer: - QgsProject.instance().removeMapLayers([self.currentLayer.id()]) + if not QgsProject.instance().layerTreeRoot().findLayer(self.currentLayer.id()): + QgsProject.instance().removeMapLayers([self.currentLayer.id()]) - if vl: + if vl and vl.isValid(): self.setLayers([vl]) QgsProject.instance().addMapLayers([vl], False) self.zoomToFullExtent()