Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions sqlx-mysql/src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,21 @@ impl Row for MySqlRow {

impl ColumnIndex<MySqlRow> for &'_ str {
fn index(&self, row: &MySqlRow) -> Result<usize, Error> {
row.column_names
.get(*self)
.ok_or_else(|| Error::ColumnNotFound((*self).into()))
.copied()
// Work around Issue #2206, <https://github.com/launchbadge/sqlx/issues/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()
}
}
}

Expand Down
33 changes: 33 additions & 0 deletions tests/mysql/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<MySql>().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(())
}
Loading