mirror of
https://github.com/launchbadge/sqlx.git
synced 2026-03-19 08:39:44 +00:00
breaking: fix name collision in FromRow, return Error::ColumnDecode for TryFrom errors (#3356)
* chore: create regression test for #3344 * fix(derives): use a parameter name that's less likely to collide * breaking(derives): emit `Error::ColumnDecode` when a `TryFrom` conversion fails in `FromRow` Breaking because `#[sqlx(default)]` on an individual field or the struct itself would have previously suppressed the error. This doesn't seem like good behavior as it could result in some potentially very difficult bugs. Instead of using `TryFrom` for these fields, just implement `From` and apply the default explicitly. * fix: run `cargo fmt` * fix: use correct field in `ColumnDecode`
This commit is contained in:
@@ -769,3 +769,26 @@ async fn test_enum_with_schema() -> anyhow::Result<()> {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "macros")]
|
||||
#[sqlx_macros::test]
|
||||
async fn test_from_row_hygiene() -> anyhow::Result<()> {
|
||||
// A field named `row` previously would shadow the `row` parameter of `FromRow::from_row()`:
|
||||
// https://github.com/launchbadge/sqlx/issues/3344
|
||||
#[derive(Debug, sqlx::FromRow)]
|
||||
pub struct Foo {
|
||||
pub row: i32,
|
||||
pub bar: i32,
|
||||
}
|
||||
|
||||
let mut conn = new::<Postgres>().await?;
|
||||
|
||||
let foo: Foo = sqlx::query_as("SELECT 1234 as row, 5678 as bar")
|
||||
.fetch_one(&mut conn)
|
||||
.await?;
|
||||
|
||||
assert_eq!(foo.row, 1234);
|
||||
assert_eq!(foo.bar, 5678);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user