In QGIS 3.2 (and 3.0), when trying to load a PostGIS layer into QGIS using a python function as follows:
def create_pg_layer(table,schema,geom_column='geom'): uri = QgsDataSourceUri() # set host name, port, database name, username and password uri.setConnection("localhost", "5432","","","") uri.setDataSource(schema, table, geom_column) vlayer = QgsVectorLayer(uri.uri(False), table, "postgres") return vlayer
It returns a
None layer object, and fails to load the layer when the PostGIS
table is empty, but succeed when the
table contains a non-zero number of rows with non-empty geometry.
This is annoying because a PostGIS table can be treated as invalid (or valid) depending on its content.
Is there a way to allow an empty PostGIS table to be loaded into QGIS in above?