I've created a tool to find layers from our PostGIS database based on a string match and add them to the current QGIS project. In the example below I'm searching for 'landscape' in the dataset name.
The basic code that adds the layers in: ## I feed in all the parameters with 'P' on the end
uri = QgsDataSourceUri() uri.setConnection(hostP, "5432", databaseP, userP, passP) uri.setDataSource (schemaP, dataChoiceNameP, geometrycolP) vlayer=QgsVectorLayer (uri.uri(False),dataChoiceNameP,'postgres') currentProject.addMapLayer(vlayer, False) mygroup.addLayer(vlayer)
Note that non of these layers originally had the ID field at the beginning and even then the same 2 out of 4 worked. Then I did a drop and create (in FME using PostGIS writer) for all datasets and putting 'id' column at the front (see settings below) and still the same two datasets didn't want to load.
When adding the layer from the QGIS Browser window, PostGIS connections the two problem datasets are added successfully, also works if I use the DB manager.
All 4 datasets have the same attribute/column data types and column names, id, geom, autocad_layer, etc. So I can't see any difference in the data to explain the problem. All datasets had their geometry checked and fixed before going into the database. These are real tables, -not views- unlike a previous post.
Q1. Any ideas why those 2 datasets don't load in the python tool?
Q2. What the difference is between the QGIS GUI (Browser panel/DB Manager) add layer code and the python equivalent?