From f85a70c76ae020ba96c406157940859d9c96860b Mon Sep 17 00:00:00 2001 From: Dean Serenevy Date: Mon, 3 Nov 2025 15:27:00 -0500 Subject: [PATCH] Work around Issue #2206 Possibly not the best fix, but for some reason MySqlRow.column_names is not being populated. If a string index is requested and column_names is empty, fall back to iterating over columns to try to find the column index. --- sqlx-mysql/src/row.rs | 19 +++++++++++++++---- tests/mysql/mysql.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/sqlx-mysql/src/row.rs b/sqlx-mysql/src/row.rs index 7bed7c7726..3cb830a60e 100644 --- a/sqlx-mysql/src/row.rs +++ b/sqlx-mysql/src/row.rs @@ -42,10 +42,21 @@ impl Row for MySqlRow { impl ColumnIndex for &'_ str { fn index(&self, row: &MySqlRow) -> Result { - row.column_names - .get(*self) - .ok_or_else(|| Error::ColumnNotFound((*self).into())) - .copied() + // Work around Issue #2206, + // + // column_names is empty so will always fail, but user expects this to work. + // Check the individual columns. + if row.column_names.is_empty() { + row.columns + .iter() + .find_map(|c| (*c.name == **self).then_some(c.ordinal)) + .ok_or_else(|| Error::ColumnNotFound((*self).into())) + } else { + row.column_names + .get(*self) + .ok_or_else(|| Error::ColumnNotFound((*self).into())) + .copied() + } } } diff --git a/tests/mysql/mysql.rs b/tests/mysql/mysql.rs index 5d6a5ef233..9e4dd70fa3 100644 --- a/tests/mysql/mysql.rs +++ b/tests/mysql/mysql.rs @@ -636,3 +636,36 @@ async fn issue_3200() -> anyhow::Result<()> { Ok(()) } + +#[sqlx_macros::test] +async fn it_can_name_columns_issue_2206() -> anyhow::Result<()> { + let mut conn = new::().await?; + + sqlx::raw_sql( + "\ + CREATE TABLE IF NOT EXISTS issue_2206 + ( + `id` BIGINT AUTO_INCREMENT, + `name` VARCHAR(128) NOT NULL, + PRIMARY KEY (id) + ); + ", + ) + .execute(&mut conn) + .await?; + + // Support for RETURNING added in MariaDB 10.5.0 and not currently (2025) + // supported in MySQL. Don't fail test due to lack of RETURNING support. + if let Ok(row) = sqlx::query("INSERT INTO issue_2206 (name) VALUES (?) RETURNING *") + .bind("Alice") + .fetch_one(&mut conn) + .await + { + let _id: i64 = row.get("id"); + let name: String = row.get("name"); + + assert_eq!(&name, "Alice"); + } + + Ok(()) +}