Bug report #13170
postgis layer.dataProvider().setDataSourceURI(.....) has no effect.
|Affected QGIS version:||master||Regression?:||No|
|Operating System:||Easy fix?:||No|
|Pull Request or Patch supplied:||No||Resolution:||wontfix|
|Crashes QGIS or corrupts data:||No||Copied to github as #:||21233|
this seems an issue at least since 2011, calling layer.dataProvider().setDataSourceURI(.....) has no effect on an existing layer.
The problem is that the URI is parsed in the ctor and never re-parsed, I think we should decide if the method has to be deprecated and used internally or fix it.
I would like to hear the opinion of the other devs about what is happening in the other data providers (does setDataSourceURI works as advertised?) and if are there any other approaches that could be taken to solve the issue.
#2 Updated by Olivier Dalang over 2 years ago
Similar issue here.
It partially works with shapefiles (the layer changed on map redraw)
layer1 = QgsVectorLayer('/path/to/a.shp') QgsProject.instance().addMapLayer(layer1) layer1.dataProvider().setDataSourceUri('/path/to/b.shp')
It doesn't work at all with geopackages (no noticeable change)
layer1 = QgsVectorLayer('/path/to/db.gpkg|layer=a') QgsProject.instance().addMapLayer(layer1) layer1.dataProvider().setDataSourceUri ('/path/to/db.gpkg|layer=b')
In both case, it seems to work only partially, as
layer1.source() still returns the initial layer. This means that the initial layer gets saved to the project file, and that changes are not persistent.
I think this is a bug, and the API would clearly benefit from a bit of clarification around data source uri.
#3 Updated by Alessandro Pasotti over 2 years ago
- Resolution set to wontfix
- Status changed from Open to Closed
- Description updated (diff)
Well, as explained in https://lists.osgeo.org/pipermail/qgis-developer/2018-September/054655.html, this is not really a bug because you should use the QgsVectorLayer API and not the provider directly. What's get saved is the QgsVectorLayer, not the data provider.
Whether the dataprovider API should be exposed or not it's not under discussion: it is very useful in many occasions.
But I agree that we could probably improve the documentation.