Skip to content

Commit

Permalink
Reset LastInsertedId to 0 between commands, not -1. Fixes #1147
Browse files Browse the repository at this point in the history
  • Loading branch information
bgrainger committed Feb 26, 2022
1 parent 4797c44 commit dcbd32a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/MySqlConnector/Core/CommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public static async Task<MySqlDataReader> ExecuteReaderAsync(IReadOnlyList<IMySq

using var payload = writer.ToPayloadData();
connection.Session.StartQuerying(command.CancellableCommand);
command.SetLastInsertedId(-1);
command.SetLastInsertedId(0);
try
{
await connection.Session.SendAsync(payload, ioBehavior, CancellationToken.None).ConfigureAwait(false);
Expand Down
21 changes: 21 additions & 0 deletions tests/SideBySide/InsertTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,27 @@ Foreign Key(foreign_id) REFERENCES TestTable(id)
}
}

[Fact]
public async Task LastInsertedIdInsertIgnore()
{
await m_database.Connection.ExecuteAsync(@"drop table if exists insert_ai;
create table insert_ai(rowid integer not null primary key auto_increment, text varchar(100) not null);
");
try
{
await m_database.Connection.OpenAsync();
using var command = new MySqlCommand(@"INSERT IGNORE INTO insert_ai (rowid, text) VALUES (2, 'test');", m_database.Connection);
Assert.Equal(1, await command.ExecuteNonQueryAsync());
Assert.Equal(2L, command.LastInsertedId);
Assert.Equal(0, await command.ExecuteNonQueryAsync());
Assert.Equal(0L, command.LastInsertedId);
}
finally
{
m_database.Connection.Close();
}
}

[Fact]
public async Task RowsAffected()
{
Expand Down

0 comments on commit dcbd32a

Please sign in to comment.