Bug report #13170

postgis layer.dataProvider().setDataSourceURI(.....) has no effect.

Added by Alessandro Pasotti over 9 years ago. Updated about 6 years ago.

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

Description

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.


Related issues

Related to QGIS Application - Feature request #8164: Can't fix bad datasource info in qgs file through GUI Open 2013-06-25
Related to QGIS Application - Feature request #8672: Add an option to modify datasource string on a query laye... Closed 2013-09-25

History

#1 Updated by Giovanni Manghi over 7 years ago

  • Easy fix? set to No
  • Regression? set to No

#2 Updated by Olivier Dalang about 6 years ago

Hi,

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.

Cheers

#3 Updated by Alessandro Pasotti about 6 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.

Also available in: Atom PDF