[MDEV-9832] Improved last_insert_id() Created: 2016-03-30 Updated: 2016-07-13 Due: 2016-04-19 Resolved: 2016-07-13 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Triggers |
| Fix Version/s: | N/A |
| Type: | Task | Priority: | Major |
| Reporter: | VAROQUI Stephane | Assignee: | Sergei Golubchik |
| Resolution: | Won't Fix | Votes: | 0 |
| Labels: | None | ||
| Issue Links: |
|
||||||||
| Description |
|
Hi MariaDB captains, Working with custom sequences different from auto increment will break applications relying on last_insert_id() The following bug may be extended into a task to globally improve management of sequences Test case
This returning 0 and not the last inserted auto increment In a more generic case it would be appreciate if last_insert_id() could return at least the single column numeric PK of the last inserted record of the statement. |
| Comments |
| Comment by Elena Stepanova [ 2016-03-30 ] | ||||||||||||||||||||||||||
|
LAST_INSERT_ID() (without an argument) is explicitly defined as a function that "returns the first automatically generated value successfully inserted for an AUTO_INCREMENT column". If applications rely on a non-existing feature, it surely does not mean that the function breaks them, it means that applications have wrong logic. I don't see any bug here whatsoever. If I understand the goal of SQL in your example, it could have been easily achieved by doing instead
Now, for the "improved management of sequences", please explain in more detail what exactly you mean by that, if there is anything other than "In a more generic case it would be appreciate if last_insert_id() could return at least the single column numeric PK of the last inserted record of the statement.". If it's just that, I can of course convert it into a feature request, but I bet it would break more (valid) applications that it would fix, because existing applications now rightfully relay on LAST_INSERT_ID() returning the last automatically generated value, and with the new semantics it would be overridden. | ||||||||||||||||||||||||||
| Comment by VAROQUI Stephane [ 2016-03-30 ] | ||||||||||||||||||||||||||
|
Hello Elnest , did you test this , that's exactly the bug i see , or did you get a working release for that test case
| ||||||||||||||||||||||||||
| Comment by Elena Stepanova [ 2016-03-30 ] | ||||||||||||||||||||||||||
|
stephane@skysql.com, In my example I suggested to use the variable that was previously set in the trigger. | ||||||||||||||||||||||||||
| Comment by VAROQUI Stephane [ 2016-03-30 ] | ||||||||||||||||||||||||||
|
"improved management of sequences", please explain in more detail what exactly you mean by that: Users looking for way to get a sequence of primary key that won't conflict on a cluster of nodes:
Similar feature will open MariaDB to multi source to single table consolidation architecture. Can be found in many offline datamart where conflicts are resolve with post synchronisation process | ||||||||||||||||||||||||||
| Comment by VAROQUI Stephane [ 2016-03-30 ] | ||||||||||||||||||||||||||
|
Indeed not a bug , following trigger looks like the type of workaround we would require in a proper implementation with native sequence. It make sens that @insert_id is not yet known in a BEFORE context of a trigger ! Thanks for the passionate chat !
| ||||||||||||||||||||||||||
| Comment by Sergei Golubchik [ 2016-07-13 ] | ||||||||||||||||||||||||||
|
last_insert_id was designed to work for auto increment. it cannot reliably work for uuid_short, think of a multi-row insert, auto-increment values can be calculated from last_insert_id, uuid values cannot be. Also, you can have non-numeric columns, e.g. for UUID(), you can have many uuid/uuid_short columns — last_insert_id cannot possibly cover all use cases. Note that in 10.2 you can have expressions in the DEFAULT clause ( |