in reply to Finding value of primary key (autoincrement) from last-inserted row with DBIx::Abstract and postgresql
If nothing else, you could always add a select max(columnname) from tablename in the same transaction as the insert to get the value back. Not necessarily pretty. I'm sure that there's a better way, but I'm not familiar with PostgreSQL.
Update: I just did a quick google, and found http://lists.initd.org/pipermail/psycopg/2003-January/001656.html in which Colin Fox describes a better way:
The Postgres 'serial' type is really a shorthand for creating a sequence, and setting the default value of the field to the NEXTVAL() of the sequence.
You can use the CURVAL() function to determine the current value of a sequence, as long as you know the name of the sequence. Postgres follows a simple naming convention for the serial numbers: tablename_fieldname_seq, so in your case the sequence would be called test_id_seq:
--roboticusselect CURVAL('test_id_seq');
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Finding value of primary key (autoincrement) from last-inserted row with DBIx::Abstract and postgresql
by ajkessel (Acolyte) on Jun 19, 2006 at 02:43 UTC | |
by roboticus (Chancellor) on Jun 19, 2006 at 03:03 UTC |