Re: nextval/dbi question
От | harrold@sage.che.pitt.edu |
---|---|
Тема | Re: nextval/dbi question |
Дата | |
Msg-id | Pine.LNX.4.21.0108051836560.10373-100000@sage.che.pitt.edu обсуждение исходный текст |
Ответ на | Re: nextval/dbi question (Horst Herb <hherb@malleenet.net.au>) |
Ответы |
Re: nextval/dbi question
Re: nextval/dbi question |
Список | pgsql-novice |
Sometime in August Horst Herb assaulted keyboard and produced... |On Monday 06 August 2001 06:55, you wrote: | |> this is what i'm doing: |> # i get the new value like this: |> select nextval('testtable_the_key_seq'); | |you are not assigning it to any variable? | |> #and then i preforme the insert like this: |> insert into testtable (an_id, timestamp) values (2, 2); | |I assume that an_id is not your primary key? | |> say the last value of the sequence is 'n-1' so the select nextval |> statement above will return 'n' but when i preform the insert the value of |> testtable.the_key is actually 'n+1'. should i wrap the insert up into a |> transaction? any help would be most appreciated. |> |> i dont know if this is relevent but in perl i'm preparing the statement by |> using (?,?) and place holders for the (2,2). | |Why do you call nextval? If your key attribute is "serial", postgres calls |nextval for you autiomatically. Thus, you call it once and postgres calls it |once, hence an increment of 2 | |Horst this is what i want to do: i want to insert data into a table that has a serialized key. then i want the value of the key for the row i just inserted. from the stuff i read on the net i though i was supposed to run the nextval command first then preform the insert. referring specifically to the url below: http://www.mail-archive.com/dbi-users@isc.org/msg15723.html i was under the impression that nextval would give what the nextval would be. i didnt think it was suppose to increment it. after reading your response and someone else's it seems i need to get the next value of the key and insert it explicitly is that correct? for what it's worth the sql to create the table: CREATE TABLE testtable( the_key serial, an_id int4, timestamp int4, PRIMARY KEY(the_key)); thanks john
В списке pgsql-novice по дате отправления: