Hi Jürgen,

Thanks for your reply.

Yesterday, I used the max+1 solution, but as you say this not optimal. I will try to define it safely.

But I already have a serial in my table: CREATE TABLE mytable (id serial NOT NULL);
I also tried to define on my own:
CREATE SEQUENCE mytable_id_seq ;
CREATE TABLE mytable (id integer DEFAULT nextval('mytable_id_seq') NOT NULL);
but it is strictly the same.

If you have any clue, it's great! I'm a little bit lost.

Isn't because QGIS is trying to write each field, ie:
INSERT INTO table (id,field1,field2,...) VALUES ('','val1','val2',...)
instead of doing
INSERT INTO table (field1,field2,...) VALUES ('val1','val2',...) ?

Btw, if you want to have a look on how I created the feature (I might have do something wrong), here at lines 95-113 https://github.com/3nids/Triangulation/blob/ba601c44282ba31c20c3484d123bff8241fbdc7e/place_arc.py

Thanks again!

Greetings,
Denis




On 01/16/2012 04:54 PM, Jürgen E. Fischer wrote:
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

_______________________________________________
Qgis-developer mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/qgis-developer

Reply via email to