Hi Stefan On Thu, Jul 28, 2011 at 10:45 AM, Stefan Ziegler <[email protected]> wrote: > Hi > > I'm trying to add some features with python to an existing postgis layer: > > tsplayer.startEditing() > tspprovider.addFeatures(features) > committed = tsplayer.commitChanges() > > The strange thing is that "committed" is "True" even if the features were not > added (because of violating a unique constraint) and dialog popped up: > > ERROR: duplicate key value violates unique constraint "tsp_lv03_nummer_key" > > Should "committed" not be "False" in this case?
In your code you are actually mixing two approaches: one possibility is to use directly data provider to store changes (they are immediately written). Or you can use vector layer's editing buffer: call startEditing(), call some methods that modify the layer and finally call either commitChanges() or rollback(). Until you commit the changes, they are only in memory. In commitChanges() they are written to the data store using provider's methods. Getting back to your code: first you start editing, then directly write features to providers (that fails) and finally commitChanges from layer's edit buffer (which is empty). That's why commit returns True - because it did nothing. What you probably want to do: - in case it is an interactive tool you should use the vector layer's editing buffer (i.e. call QgsVectorLayer's editing methods). Also you might make your tool available only when the layer is in editing mode and leave the decision when to start editing / commit / rollback up to user - the user then may use also undo/redo functionality. - if you are working on a data processing tool you probably want to store the features directly without messing with the layer editing buffer - then you are better off with using vector data provider. Regards Martin _______________________________________________ Qgis-developer mailing list [email protected] http://lists.osgeo.org/mailman/listinfo/qgis-developer
