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 12 years ago. Updated over 12 years ago.

Status:Closed
Priority:Normal
Assignee:-
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

Description

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')
try:
    #if sql does not return data - get PostGIS error, but not exception?
    vlayer = QgsVectorLayer(uri.uri(), layerName, "postgres")
except:
    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?

History

#1 Updated by Jürgen Fischer over 12 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