This is a general question. I'm stepping through the pyqgis code and the layer isValid() is always False. I don't see any error messages showing any reason why it wouldn't load. Other similars have loaded without problems.

Just trying to see if I need to actually download the QGIS source and start stepping through that as well...? (i hope not)

the first layer loads without problems. it's referencing a postgis table. the second doesn't but the another_table is a view. perhaps that makes a difference but there are no error messages or exceptions letting me know what's wrong...

Nothing fancy:

    #first vLayer
    self.first_layer = QgsVectorLayer(self.uri.uri(), 'Some Locations', "postgres")


    #second Layer
    self.uri.setDataSource('public', 'another_table', 'location','')
    self.second_layer = QgsVectorLayer(self.uri.uri(), 'Another set of Locations', "postgres")

  • Can you post some code? – Nathan W Jan 7 '15 at 3:25
  • looks like when i changed the postgis view to a table it worked, i.e., the layer.isValid() was fine. So i guess QGIS 2.4 doesn't support postgres views? – JMI Jan 7 '15 at 22:13
  • to create the table i did this: create table some_table as select * from some_view; – JMI Jan 7 '15 at 22:14

I think you need to provide a field name to be used as primary key by QGIS:

self.uri.setDataSource('public', 'another_table', 'location','', 'key_field')

Of course, make sure 'key_field' exists in your view and is a unique field, or else, create a field with such characteristics in your view.

If that doesn't work, see How to debug 'Layer is not valid error' from Python plugin?, and try passing the SQL that defines your view directly to setDataSource, like this:

query = 'SELECT key_field, location, ... FROM ... WHERE ...'
self.uri.setDataSource('', '(' + query + ')', 'location', '', 'key_field')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.