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

Bug #19918 lastInsertID(): Using lastval() breaks triggers in postgresql!
Submitted: 2013-04-29 18:39 UTC
From: mirkovogt Assigned:
Status: Open Package: MDB2 (version 2.5.0b5)
PHP Version: irrelevant OS: irrelevant
Roadmaps: (Not assigned)    
Subscription  


 [2013-04-29 18:39 UTC] mirkovogt (Mirko Vogt)
Description: ------------ The function 'lastInsertID' in /usr/share/php/MDB2/Driver/pgsql.php is supposed to return the ID of the previous change (mostly 'INSERT') by the application using this pgsql-connection. This however isn't always true. lastInsertID() is either using currval($arg) for a given table/sequence or lastval() if no parameter got passed. Using currval($arg) is totally fine, however using lastval() is highly discouraged and considered to be racy. lastval() returns the ID of the very last change on this set/connection, not necessarily done by the application itself. An example: I'm having the table 'foo' and a trigger configured on it, which is causing an INSERT to table 'bar' on any change to 'foo'. Now I'm doing sth. like: INSERT INTO 'foo'; SELECT lastval(); However in this case 'lastval()' doesn't return the ID of the INSERT my application caused, but the INSERT the trigger caused. This isn't just a theoretical issue: I'm using slony for replication of my postgres tables and the application using lastInsertID() reproducible get back the the ID of the INSERT caused by the trigger, not my application. Since there is no way of using currval() without any argument, I don't have an ideal solution not changing the API. So I propose to NOT allow using lastInsertID() anymore without any arguments! Expected result: ---------------- lastInsertID() returning the ID of the last change _I_ made Actual result: -------------- lastInsertID() returning the ID of the last change on this set and connection (which might be caused by e.g. triggers)

Comments

 [2013-04-29 19:01 UTC] mirkovogt (Mirko Vogt)
-Operating System: Linux +Operating System: irrelevant -PHP Version: 5.3.10 +PHP Version: irrelevant