<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
Hi Jürgen,<br>
<br>
Thanks for your reply.<br>
<br>
Yesterday, I used the max+1 solution, but as you say this not
optimal. I will try to define it safely.<br>
<br>
But I already have a serial in my table: CREATE TABLE mytable (id
serial NOT NULL);<br>
I also tried to define on my own:<br>
CREATE SEQUENCE mytable_id_seq ;<br>
CREATE TABLE mytable (id integer DEFAULT nextval('mytable_id_seq')
NOT NULL);<br>
but it is strictly the same.<br>
<br>
If you have any clue, it's great! I'm a little bit lost. <br>
<br>
Isn't because QGIS is trying to write each field, ie: <br>
INSERT INTO table (id,field1,field2,...) VALUES
('','val1','val2',...)<br>
instead of doing<br>
INSERT INTO table (field1,field2,...) VALUES ('val1','val2',...) ?<br>
<br>
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<br>
<meta http-equiv="content-type" content="text/html;
charset=ISO-8859-1">
<a
href="https://github.com/3nids/Triangulation/blob/ba601c44282ba31c20c3484d123bff8241fbdc7e/place_arc.py">https://github.com/3nids/Triangulation/blob/ba601c44282ba31c20c3484d123bff8241fbdc7e/place_arc.py</a>
<br>
<br>
Thanks again!<br>
<br>
Greetings, <br>
Denis<br>
<br>
<br>
<br>
<br>
On 01/16/2012 04:54 PM, Jürgen E. Fischer wrote:
<blockquote cite="mid:20120116155428.GA2358@norbit.de" type="cite">
<pre wrap="">Hi Denis,
On Mon, 16. Jan 2012 at 13:04:01 +0100, Denis Rouzaud wrote:
</pre>
<blockquote type="cite">
<pre wrap="">I am trying to add a feature to a PostGIS table, in which there is a
serial primary key 'id'.
</pre>
</blockquote>
<pre wrap="">
</pre>
<blockquote type="cite">
<pre wrap="">Without defining id, here is what I get when I try addFeatures:
ERROR: null value in column "id" violates not-null constraint
</pre>
</blockquote>
<pre wrap="">
You could setup a DEFAULT value for the id column in PostgreSQL (like SERIAL
does it).
</pre>
<blockquote type="cite">
<pre wrap="">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)"
</pre>
</blockquote>
<pre wrap="">
QGIS interprets that as string and tries to stuff it into the integer column
which obviously fails.
</pre>
<blockquote type="cite">
<pre wrap="">Is there a bug in the provider, or should I get first the new id (if
yes, how do I do this) ?
</pre>
</blockquote>
<pre wrap="">
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
</pre>
</blockquote>
</body>
</html>