Skip to content

Commit dcbd32a

Browse files
committed
Reset LastInsertedId to 0 between commands, not -1. Fixes #1147
1 parent 4797c44 commit dcbd32a

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/MySqlConnector/Core/CommandExecutor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static async Task<MySqlDataReader> ExecuteReaderAsync(IReadOnlyList<IMySq
4949

5050
using var payload = writer.ToPayloadData();
5151
connection.Session.StartQuerying(command.CancellableCommand);
52-
command.SetLastInsertedId(-1);
52+
command.SetLastInsertedId(0);
5353
try
5454
{
5555
await connection.Session.SendAsync(payload, ioBehavior, CancellationToken.None).ConfigureAwait(false);

tests/SideBySide/InsertTests.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,27 @@ Foreign Key(foreign_id) REFERENCES TestTable(id)
185185
}
186186
}
187187

188+
[Fact]
189+
public async Task LastInsertedIdInsertIgnore()
190+
{
191+
await m_database.Connection.ExecuteAsync(@"drop table if exists insert_ai;
192+
create table insert_ai(rowid integer not null primary key auto_increment, text varchar(100) not null);
193+
");
194+
try
195+
{
196+
await m_database.Connection.OpenAsync();
197+
using var command = new MySqlCommand(@"INSERT IGNORE INTO insert_ai (rowid, text) VALUES (2, 'test');", m_database.Connection);
198+
Assert.Equal(1, await command.ExecuteNonQueryAsync());
199+
Assert.Equal(2L, command.LastInsertedId);
200+
Assert.Equal(0, await command.ExecuteNonQueryAsync());
201+
Assert.Equal(0L, command.LastInsertedId);
202+
}
203+
finally
204+
{
205+
m_database.Connection.Close();
206+
}
207+
}
208+
188209
[Fact]
189210
public async Task RowsAffected()
190211
{

0 commit comments

Comments
 (0)