currval(seq) fails if no nextval(seq) first

From: Louis-David Mitterrand <cunctator(at)apartia(dot)ch>
To: pgsql-general(at)postgresql(dot)org
Subject: currval(seq) fails if no nextval(seq) first
Date: 2000-07-20 08:54:50
Message-ID: 20000720105449.A1860@styx
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello,

I am trying to select the most recently inserted row in a table by
matching the id column with the current value of the sequence that
generated the id (I know this is not the best way to do it, because the
sequence might have holes in it). By the way what is the canonical way
of selecting the most recently inserted row in a table?

The strange thing is that currval() fails until nextval() is called on
the sequence. Is this normal?

Thanks in advance,

auction=# SELECT * FROM auction* WHERE login = 'vindex' AND id = currval('auction_id_seq');
ERROR: auction_id_seq.currval is not yet defined in this session
auction=# select nextval('auction_id_seq');
nextval
---------
67
(1 row)

auction=# SELECT * FROM auction* WHERE login = 'vindex' AND id = currval('auction_id_seq');
id | login | startdate | stopdate | description | startprice | reserveprice | category | imageurl | title | quantity | created | modified | option | auto_renew | renew_count | private | dutch | accept_visa | accept_amex | accept_cheque | accept_bank_transfer | accept_bank_cheque | ship_international | buyer_pays_shipping | currency
----+-------+-----------+----------+-------------+------------+--------------+----------+----------+-------+----------+---------+----------+--------+------------+-------------+---------+-------+-------------+-------------+---------------+----------------------+--------------------+--------------------+---------------------+----------
(0 rows)

--
Louis-David Mitterrand - ldm(at)apartia(dot)org - http://www.apartia.fr

Radioactive cats have 18 half-lives.

Browse pgsql-general by date

  From Date Subject
Next Message Karel Zak 2000-07-20 09:02:50 Re: Re: [HACKERS] 8Ko limitation
Previous Message Jules Bean 2000-07-20 08:52:01 Re: Re: [HACKERS] 8Ko limitation