-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Match MySQL's LAST_INSERT_ID
behaviour when no rows are inserted / updated
#15699
base: main
Are you sure you want to change the base?
Changes from all commits
3ccff20
0d8494a
fbf13de
c053a3a
7bee257
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -174,9 +174,20 @@ func (ins *Insert) executeInsertQueries( | |
return nil, vterrors.Aggregate(errs) | ||
} | ||
|
||
// If this insert used auto increment values from a sequence, we need to set the `last_insert_id` value. | ||
if insertID != 0 { | ||
result.InsertID = insertID | ||
if result.RowsAffected > 0 { | ||
// If at least one row was affected, we set the `last_insert_id` value to the lowest reserved sequence id. | ||
// | ||
// This does not match the behaviour of MySQL in case where no new rows where inserted (but one or more rows were updated | ||
// via `ON DUPLICATE KEY UPDATE`), where the `last_insert_id` value is set to the `auto_increment` column value. | ||
result.InsertID = insertID | ||
} else { | ||
// If no rows were inserted or updated, clear the `last_insert_id` value. | ||
result.InsertID = 0 | ||
} | ||
} | ||
|
||
Comment on lines
+177
to
+190
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is not true. I tried it on mysql mysql> insert into at(col) values (20);
Query OK, 1 row affected (0.00 sec)
mysql> select * from at where col = 20;
+----+------+
| id | col |
+----+------+
| 10 | 20 |
+----+------+
1 row in set (0.00 sec)
mysql> insert into at(col) values (20);
ERROR 1062 (23000): Duplicate entry '20' for key 'at.col'
mysql> insert into at(col) values (20) on duplicate key update col = 40;
Query OK, 2 rows affected (0.00 sec)
mysql> select * from at where col = 40;
+----+------+
| id | col |
+----+------+
| 10 | 40 |
+----+------+
1 row in set (0.00 sec)
mysql> select @@last_insert_id;
+------------------+
| @@last_insert_id |
+------------------+
| 10 |
+------------------+
1 row in set (0.00 sec) If you follow through this sample test. The last_insert_id remained the old value of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should never reset the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. another case where row_affected is mysql> insert into at(col) select 10 from dual where 1!=1;
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> select @@last_insert_id;
+------------------+
| @@last_insert_id |
+------------------+
| 10 |
+------------------+
1 row in set (0.00 sec) |
||
return result, nil | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We do not need to test with way, mcmp.Exec can validate all of this.
With latest changes to this test package we can add new compare option