4

I am attempting to create a QgsVectorLayer with the same code as I am using for a PostgreSQL table, which works correctly and the isValid() method on the layer returns true.

uri=QgsDataSourceUri()
uri.setConnection("localhost","5433","xxx","postgres","xxx",QgsDataSourceUri.SslDisable)
uri.setDataSource("xxx","sv_demog","geom","id")
uri.setSrid("4269")
vlayer=QgsVectorLayer(uri.uri(),"xxxx","postgres")
print('sv_demog layer is valid: %s' % (vlayer.isValid()))

The view selects from table where id is the primary key, and has a geometry column.

How do I make this work for a view?

I am using a PostgreSQL 10 database, and QGIS 3 (Update: Same behavior in 2.18).

Ref: Loading Layers

asked Jan 3, 2019 at 12:11

1 Answer 1

4

Jay, I had the same problem. You have to specify the view's primary key. For example, uri.setDataSource('my schema', 'my_view', 'geom', '', 'id')

Check out this thread postGIS views, qGIS, qGIS python console

answered Feb 5, 2019 at 10:17

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.