File tree Expand file tree Collapse file tree 2 files changed +48
-4
lines changed Expand file tree Collapse file tree 2 files changed +48
-4
lines changed Original file line number Diff line number Diff line change @@ -42,10 +42,21 @@ impl Row for MySqlRow {
4242
4343impl ColumnIndex < MySqlRow > for & ' _ str {
4444 fn index ( & self , row : & MySqlRow ) -> Result < usize , Error > {
45- row. column_names
46- . get ( * self )
47- . ok_or_else ( || Error :: ColumnNotFound ( ( * self ) . into ( ) ) )
48- . copied ( )
45+ // Work around Issue #2206, <https://github.com/launchbadge/sqlx/issues/2206>
46+ //
47+ // column_names is empty so will always fail, but user expects this to work.
48+ // Check the individual columns.
49+ if row. column_names . is_empty ( ) {
50+ row. columns
51+ . iter ( )
52+ . find_map ( |c| ( * c. name == * * self ) . then_some ( c. ordinal ) )
53+ . ok_or_else ( || Error :: ColumnNotFound ( ( * self ) . into ( ) ) )
54+ } else {
55+ row. column_names
56+ . get ( * self )
57+ . ok_or_else ( || Error :: ColumnNotFound ( ( * self ) . into ( ) ) )
58+ . copied ( )
59+ }
4960 }
5061}
5162
Original file line number Diff line number Diff line change @@ -636,3 +636,36 @@ async fn issue_3200() -> anyhow::Result<()> {
636636
637637 Ok ( ( ) )
638638}
639+
640+ #[ sqlx_macros:: test]
641+ async fn it_can_name_columns_issue_2206 ( ) -> anyhow:: Result < ( ) > {
642+ let mut conn = new :: < MySql > ( ) . await ?;
643+
644+ sqlx:: raw_sql (
645+ "\
646+ CREATE TABLE IF NOT EXISTS issue_2206
647+ (
648+ `id` BIGINT AUTO_INCREMENT,
649+ `name` VARCHAR(128) NOT NULL,
650+ PRIMARY KEY (id)
651+ );
652+ " ,
653+ )
654+ . execute ( & mut conn)
655+ . await ?;
656+
657+ // Support for RETURNING added in MariaDB 10.5.0 and not currently (2025)
658+ // supported in MySQL. Don't fail test due to lack of RETURNING support.
659+ if let Ok ( row) = sqlx:: query ( "INSERT INTO issue_2206 (name) VALUES (?) RETURNING *" )
660+ . bind ( "Alice" )
661+ . fetch_one ( & mut conn)
662+ . await
663+ {
664+ let _id: i64 = row. get ( "id" ) ;
665+ let name: String = row. get ( "name" ) ;
666+
667+ assert_eq ! ( & name, "Alice" ) ;
668+ }
669+
670+ Ok ( ( ) )
671+ }
You can’t perform that action at this time.
0 commit comments