Skip to content

Commit 76357a1

Browse files
committed
Add multiple statement tests for LastInsertedId.
1 parent 1b2757d commit 76357a1

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

docs/content/tutorials/migrating-from-connector-net.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ The following bugs in Connector/NET are fixed by switching to MySqlConnector. (~
260260
* [#96500](https://bugs.mysql.com/bug.php?id=96500): `MySqlDataReader.GetFieldValue<MySqlGeometry>` throws `InvalidCastException`
261261
* [#96636](https://bugs.mysql.com/bug.php?id=96636): `MySqlConnection.Open()` slow under load when using SSL
262262
* [#96717](https://bugs.mysql.com/bug.php?id=96717): Not compatible with MySQL Server 5.0
263+
* [#97061](https://bugs.mysql.com/bug.php?id=97061): `MySqlCommand.LastInsertedId` returns 0 after executing multiple statements
263264
* [#97067](https://bugs.mysql.com/bug.php?id=97067): Aggregate functions on BIT(n) columns return wrong result
264265
* ~~[#97300](https://bugs.mysql.com/bug.php?id=97300): `GetSchemaTable()` returns table for stored procedure with output parameters~~
265266
* ~~[#97448](https://bugs.mysql.com/bug.php?id=97448): Connecting fails if more than one IP is found in DNS for a named host~~

tests/SideBySide/InsertTests.cs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,58 @@ public async Task LastInsertedIdTwoInserts()
144144
}
145145
}
146146

147+
[SkippableFact(Baseline = "https://bugs.mysql.com/bug.php?id=97061")]
148+
public async Task LastInsertedIdLockTables()
149+
{
150+
await m_database.Connection.ExecuteAsync(@"drop table if exists insert_ai;
151+
create table insert_ai(rowid integer not null primary key auto_increment, text varchar(100) not null);
152+
");
153+
try
154+
{
155+
await m_database.Connection.OpenAsync();
156+
using var command = new MySqlCommand(@"LOCK TABLES insert_ai WRITE;
157+
INSERT INTO insert_ai (text) VALUES ('test');
158+
UNLOCK TABLES;", m_database.Connection);
159+
await command.ExecuteNonQueryAsync();
160+
Assert.Equal(1L, command.LastInsertedId);
161+
}
162+
finally
163+
{
164+
m_database.Connection.Close();
165+
}
166+
}
167+
168+
[SkippableFact(Baseline = "https://bugs.mysql.com/bug.php?id=97061")]
169+
public async Task LastInsertedIdInsertForeignKey()
170+
{
171+
await m_database.Connection.ExecuteAsync(@"drop table if exists TestTableWithForeignKey;
172+
drop table if exists TestTable;
173+
174+
Create Table TestTable(
175+
id BIGINT NOT NULL AUTO_INCREMENT,
176+
column1 CHAR(100),
177+
Primary Key(id)
178+
);
179+
180+
Create Table TestTableWithForeignKey(
181+
foreign_id BIGINT NOT NULL,
182+
column2 CHAR(100),
183+
Foreign Key(foreign_id) REFERENCES TestTable(id)
184+
);");
185+
try
186+
{
187+
await m_database.Connection.OpenAsync();
188+
using var command = new MySqlCommand(@"INSERT INTO TestTable(column1) VALUES('hello');
189+
INSERT INTO TestTableWithForeignKey(foreign_id, column2) VALUES(LAST_INSERT_ID(), 'test');", m_database.Connection);
190+
await command.ExecuteNonQueryAsync();
191+
Assert.Equal(1L, command.LastInsertedId);
192+
}
193+
finally
194+
{
195+
m_database.Connection.Close();
196+
}
197+
}
198+
147199
[Fact]
148200
public async Task RowsAffected()
149201
{

0 commit comments

Comments
 (0)