[Qgis-developer] addFeatures without knowing new feature's id, bug?

Jürgen E. Fischer jef at norbit.de
Mon Jan 16 10:54:28 EST 2012


Hi Denis,

On Mon, 16. Jan 2012 at 13:04:01 +0100, Denis Rouzaud wrote:
> I am trying to add a feature to a PostGIS table, in which there is a  
> serial primary key 'id'.

> Without defining id, here is what I get when I try addFeatures:
> ERROR: null value in column "id" violates not-null constraint

You could setup a DEFAULT value for the id column in PostgreSQL (like SERIAL
does it).

> and I tried
> iid  = self.layer.dataProvider().fieldNameIndex('id')
> if iid != -1:  
> f.addAttribute(iid,'nextval(distribution.dimension_id_seq::regclass)')
> which leaded to
> ERROR: invalid input syntax for integer:  
> "nextval(distribution.dimension_id_seq::regclass)"

QGIS interprets that as string and tries to stuff it into the integer column
which obviously fails.

> Is there a bug in the provider, or should I get first the new id (if  
> yes, how do I do this) ?

You could use QgsDataProvider::maximumValue() to fetch it and increment it
yourself.  But that might still produce concurrency problems, if multiple people
work in parallel.  I'd stick with a DEFAULT in PostgreSQL.


Jürgen

-- 
Jürgen E. Fischer         norBIT GmbH               Tel. +49-4931-918175-20
Dipl.-Inf. (FH)           Rheinstraße 13            Fax. +49-4931-918175-50
Software Engineer         D-26506 Norden               http://www.norbit.de

-- 
norBIT Gesellschaft fuer Unternehmensberatung und Informationssysteme mbH
Rheinstrasse 13, 26506 Norden
GF: Jelto Buurman, HR: Amtsgericht Emden, HRB 5502



More information about the Qgis-developer mailing list