sqlx: Implementation of `sqlx::Decode` is not general enough
The error is generated when i’m using a struct with sqlx::Type and some field is an Option. i tried to decode manually my struct but the error is still there.
Sample sql, structs and query
`sql create table demo_seller ( id int not null constraint demo_seller_pk primary key username varchar(30) not null, first_name varchar(30) not null, last_name varchar(30) not null, photo varchar(150) );
create table demo_shop ( shop_id int not null constraint demo_shop_pk primary key, created_at timestamptz default now() not null, price numeric(15,3) not null, seller_id int not null ); `
`rust #[derive(Serialize, Deserialize, FromRow, sqlx::Type)] pub struct Seller { id: i32, username: String, first_name: String, last_name: String, photo: Option< String >, }
#[derive(Serialize, Deserialize, FromRow)] pub struct Shop{ shop_id: i32, price: Decimal, seller: Seller }
let shops: Vec<Shop> = sqlx::query_as!(Shop, r#" SELECT s.shop_id, s.price, (sl.id, sl.username, sl.first_name, sl.last_name, sl.photo) as “seller!:Seller” FROM demo_shop s inner join demo_seller sl on s.seller_id = sl.id "#).fetch_all(&*pool).await?; `
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Implementation of sqlx::Decode is not general enough
0 |
/ pub trait Decode<'r, DB: Database>: Sized {
61 | | /// Decode a new value of this type using a raw value from the database.
62 | | fn decode(value: <DB as HasValueRef<'r>>::ValueRef) -> Result<Self, BoxDynError>;
63 | | }
| |_- trait sqlx::Decode defined here
|
= note: std::option::Option<std::string::String> must implement sqlx::Decode<'0, Postgres>, for any lifetime '0…
= note: …but std::option::Option<std::string::String> actually implements sqlx::Decode<'1, Postgres>, for some specific lifetime '1
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 8
- Comments: 26 (9 by maintainers)
Commits related to this issue
- Create idempotency table We are using a postgres composite type for our headers. We can't do the same for the response as a whole due to a bug in sqlx. See https://github.com/launchbadge/sqlx/issues/... — committed to bendoerry/zero2prod by bendoerry 2 years ago
- fix: Decode and Encode derives (#1031) — committed to benluelo/sqlx by benluelo a year ago
- fix: Decode and Encode derives (#1031) — committed to benluelo/sqlx by benluelo a year ago
- fix: Decode and Encode derives (#1031) — committed to benluelo/sqlx by benluelo a year ago
- fix: Decode and Encode derives (#1031) — committed to benluelo/sqlx by benluelo a year ago
- fix: Decode and Encode derives (#1031) — committed to asteinba/sqlx by benluelo a year ago
- fix: Decode and Encode derives (#1031) — committed to benluelo/sqlx by benluelo a year ago
Finally was able to reduce this further and posted a rust issue: https://github.com/rust-lang/rust/issues/82219
Hi @frantisek-heca, finally we don’t use sqlx anyway. Have a look at Teo. This web framework has builtin ORM support. Write CRUD and aggregates are very fast with it. The framework handles the encoding and decoding for you.