Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.5.0b4

Request #9989 lastInsertID with no table / field specified
Submitted: 2007-01-31 15:25 UTC
From: mario dot adam at schaeffler dot com Assigned: quipo
Status: Closed Package: MDB2_Driver_pgsql (version 1.3.0)
PHP Version: Irrelevant OS:
Roadmaps: (Not assigned)    
Comments Add Comment Add patch

Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know! Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem : 39 - 9 = ?

 [2007-01-31 15:25 UTC] mario dot adam at schaeffler dot com (Mario Adam)
Description: ------------ The function lastInsertID() returns the result vom CURRVAL for a specified sequence. The function expects two optional parameters $table and $field which both are set to null per default. Calling this function with no parameters (and therefor use null for both $table and $field) would result in CURRVAL('_seq'), if '_seq' was the suffix for sequences. As long you do not have a sequence called like that, an error will be returned. I think, it has to be dicussed, if it wasn't better to return the last used ID at all. LASTVAL could be used for that. A possible solution is pasted in "Test script" below... Test script: --------------- function lastInsertID($table = null, $field = null) { if (empty($table) && empty($field)) { return $this->queryOne("SELECT lastval()", 'integer'); } else { $seq = $table.(empty($field) ? '' : '_'.$field); $sequence_name = $this->getSequenceName($seq); return $this->queryOne("SELECT currval('$sequence_name')", 'integer'); } }


 [2007-03-12 12:38 UTC] quipo (Lorenzo Alberton)
This bug has been fixed in CVS. If this was a documentation problem, the fix will appear on by the end of next Sunday (CET). If this was a problem with the website, the change should be live shortly. Otherwise, the fix will appear in the package's next release. Thank you for the report and for helping us make PEAR better.
 [2013-04-29 18:59 UTC] mirkovogt (Mirko Vogt)
Using 'lastval()' is a really bad idea, since its use is considered racy. I hit a concrete issue with using lastval() described in the bug report #19918. Please consider removing the lastval() call as it breaks the usage of extended features in PostgreSQL (as triggers) and isn't transparent to the application anymore (using triggers the app using lastval() doesn't (always) work, not using triggers doesn't cause issues within the app).