-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
last_insert_id() doesn't work with auto_random #15140
Comments
It appears that it's possible to have an auto_random and auto_increment column on the same table. In that case, last_insert_id() will give the value allocated to the auto_increment column. Since auto_random is meant as a replacement for auto_increment to avoid hotspots, I think it would make sense for last_insert_id() to return the last auto-random value. If there is serious concern about confusing behavior in the case of a table that has both auto_increment and auto_random columns, we'll have to consider introducing some configuration option, or maybe even DDL that would mark which column in the table should affect last_insert_id(). |
autoincrement and autorandom should probably be mutually exclusive at a table level |
@mightyguava You're right, this should be fixed too. Thank you @kolbe |
the same issue with sequence primary key: mysql> CREATE SEQUENCE `orders_seq` start with 1000;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> CREATE TABLE `orders` (
-> `id` bigint(20) NOT NULL DEFAULT nextval(`orders_seq`),
-> `name` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
-> PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */
-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
Query OK, 0 rows affected (0.12 sec)
mysql> insert into orders(name) values ('a');
Query OK, 1 row affected (0.03 sec)
mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 0 |
+------------------+
1 row in set (0.01 sec)
mysql> select lastval(orders_seq);
+---------------------+
| lastval(orders_seq) |
+---------------------+
| 1000 |
+---------------------+
1 row in set (0.00 sec)
mysql> select * from orders;
+------+------+
| id | name |
+------+------+
| 1000 | a |
+------+------+
1 row in set (0.00 sec) |
Bug Report
Please answer these questions before submitting your issue. Thanks!
If possible, provide a recipe for reproducing the error.
last_insert_id() should show the last-generated auto-random value.
tidb-server -V
or runselect tidb_version();
on TiDB)?The text was updated successfully, but these errors were encountered: