[Qgis-developer] Python QgsVectorLayer.commitChanges() problem

Stefan Ziegler st.ziegler at gmx.net
Thu Jul 28 11:18:01 EDT 2011


Hi Martin

thanks for clearing this up and for the hints.

regards
Stefan

-------- Original-Nachricht --------
> Datum: Thu, 28 Jul 2011 13:39:39 +0200
> Von: Martin Dobias <wonder.sk at gmail.com>
> An: Stefan Ziegler <st.ziegler at gmx.net>
> CC: qgis-developer at lists.osgeo.org
> Betreff: Re: [Qgis-developer] Python QgsVectorLayer.commitChanges() problem

> Hi Stefan
> 
> On Thu, Jul 28, 2011 at 10:45 AM, Stefan Ziegler <st.ziegler at gmx.net>
> 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

-- 
NEU: FreePhone - 0ct/min Handyspartarif mit Geld-zurück-Garantie!		
Jetzt informieren: http://www.gmx.net/de/go/freephone


More information about the Qgis-developer mailing list