Bug report #6074

Using QgsDataSourceURI to load a PostGIS table causes error if query returns empty and Geometry type is GEOMETRY

Added by Ian Packham over 8 years ago. Updated over 8 years ago.

Category:Data Provider/PostGIS
Affected QGIS version:master Regression?:No
Operating System: Easy fix?:No
Pull Request or Patch supplied:No Resolution:invalid
Crashes QGIS or corrupts data:No Copied to github as #:15431


I am using the following code to load a vector layer from a PostGIS table:

uri = QgsDataSourceURI()
uri.setConnection(self.host, str(self.port), self.database, self.username, self.password)
uri.setDataSource(schema, postGisTable, "the_geom",sqlstmt,'gid')
    #if sql does not return data - get PostGIS error, but not exception?
    vlayer = QgsVectorLayer(uri.uri(), layerName, "postgres")
    QMessageBox.critical(self.iface.mainWindow(), "Error", "Problem setting QgsVectorLayer")
if not vlayer.isValid():
    QMessageBox.critical(self.iface.mainWindow(),"Error","Layer failed to load!")

If the sqlstmt returns an empty query, in some cases my plugin shows the "Layer failed to load!" message meaning the layer is invalid (but is never caught by the previous try block).
Error in the General Log Messages is:

Geometry type and srid for empty column the_geom of "cmt"."subbasinsoilsdrainage_eastern_union" undefined.

On investigation I have discovered that if the Geometry type of the PostGIS layer is the generic GEOMETRY type, then I get this error. If the Geometry type is MultiPolygon or MultiPoint, then an empty layer works OK.

If this is expected behaviour then should we avoid the GEOMETRY type in our PostGIS tables?


#1 Updated by Jürgen Fischer over 8 years ago

  • Resolution set to invalid
  • Status changed from Open to Closed

with empty tables that don't have a specific geometry type and/or you have to specify the expected geometry type with QgsDataSource::setWkbType() and expected srid with QgsDataSource::setSrid()

Also available in: Atom PDF