[MDEV-26037] RETURNING clause get the wrong last_insert_id() value Created: 2021-06-29  Updated: 2021-07-01  Resolved: 2021-07-01

Status: Closed
Project: MariaDB Server
Component/s: Server
Affects Version/s: 10.5.9
Fix Version/s: N/A

Type: Bug Priority: Minor
Reporter: Zichao Lin Assignee: Unassigned
Resolution: Not a Bug Votes: 0
Labels: None
Environment:

centos



 Description   

Using RETURNING get the wrong last_insert_id(). I am not sure if this is a correct semantic.

How to repeat:

MariaDB [test]> create table t (id INT NOT NULL auto_increment, s varchar(255),primary key(id));
Query OK, 0 rows affected (0.01 sec)
 
MariaDB [test]> insert into t (s) values ("abc");
Query OK, 1 row affected (0.00 sec)
 
MariaDB [test]> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
|                1 |
+------------------+
1 row in set (0.00 sec)
 
MariaDB [test]> insert into t (s) values ("bcd") returning *, last_insert_id();
+----+------+------------------+
| id | s    | last_insert_id() |
+----+------+------------------+
|  2 | bcd  |                1 |
+----+------+------------------+
1 row in set (0.01 sec)
 
MariaDB [test]> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
|                2 |
+------------------+
1 row in set (0.00 sec)
 
MariaDB [test]> 

As we can see, we can't get the latest last_insert_id() using returning.



 Comments   
Comment by Alice Sherepa [ 2021-07-01 ]

This is not a bug as "The currently executing statement does not affect the value of LAST_INSERT_ID()" https://mariadb.com/kb/en/last_insert_id/.

Generated at Thu Feb 08 09:42:16 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.