Rust `sqlx` 抱怨缺少 trait 实现

问题描述

我有以下插入查询

   pub async fn create_property(
        &self,property: Property,) -> Result<PropertyId,sqlx::Error> {
        /* acquire connection from the pool */
        let mut conn = self.pool.acquire().await?;

        /* insert the property and retrieve its ID */
        let id = sqlx::query(
            r#"INSERT INTO properties (
                address_unit_number,address_street_number,address_street,address_suburb,address_state,address_postcode,area,property_type,available,num_bedrooms,num_bathrooms,num_garages
               ) VALUES (?1,?2,?3,?4,?5,?6,?7,?8,?9,?10,?11,?12);"#)
            .bind(property.address.unit_number)
            .bind(property.address.street_number)
            .bind(property.address.street)
            .bind(property.address.suburb)
            .bind(property.address.state)
            .bind(property.address.postcode)
            .bind(property.area)
            .bind(property.property_type)
            .bind(property.available)
            .bind(property.num_bedrooms)
            .bind(property.num_bathrooms)
            .bind(property.num_garages)
        .execute(&mut conn)
        .await?
        .last_insert_rowid();

        Ok(id)
    }

编译器每次调用 bind 时都会抱怨:

$ cargo run
   Compiling proj v0.1.0 (/home/user/proj)
error[E0277]: the trait bound `std::option::Option<u8>: Encode<'_,_>` is not satisfied
  --> src/db.rs:44:19
   |
44 |             .bind(property.address.unit_number)
   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Encode<'_,_>` is not implemented for `std::option::Option<u8>`
   |
   = help: the following implementations were found:
             <std::option::Option<T> as Encode<'q,Postgres>>
             <std::option::Option<T> as Encode<'q,sqlite>>

error[E0277]: the trait bound `u8: Type<_>` is not satisfied
  --> src/db.rs:44:19
   |
44 |             .bind(property.address.unit_number)
   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Type<_>` is not implemented for `u8`
   |
   = note: required because of the requirements on the impl of `Type<_>` for `std::option::Option<u8>`

error[E0277]: the trait bound `u64: Encode<'_,_>` is not satisfied
  --> src/db.rs:45:19
   |
45 |             .bind(property.address.street_number)
   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Encode<'_,_>` is not implemented for `u64`

error[E0277]: the trait bound `u64: Type<_>` is not satisfied
  --> src/db.rs:45:19
   |
45 |             .bind(property.address.street_number)
   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Type<_>` is not implemented for `u64`

error[E0277]: the trait bound `Addressstate: Encode<'_,_>` is not satisfied
  --> src/db.rs:48:19
   |
48 |             .bind(property.address.state)
   |                   ^^^^^^^^^^^^^^^^^^^^^^ the trait `Encode<'_,_>` is not implemented for `Addressstate`

error[E0277]: the trait bound `Addressstate: Type<_>` is not satisfied
  --> src/db.rs:48:19
   |
48 |             .bind(property.address.state)
   |                   ^^^^^^^^^^^^^^^^^^^^^^ the trait `Type<_>` is not implemented for `Addressstate`

error[E0277]: the trait bound `u64: Encode<'_,_>` is not satisfied
  --> src/db.rs:49:19
   |
49 |             .bind(property.address.postcode)
   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Encode<'_,_>` is not implemented for `u64`

error[E0277]: the trait bound `u64: Type<_>` is not satisfied
  --> src/db.rs:49:19
   |
49 |             .bind(property.address.postcode)
   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Type<_>` is not implemented for `u64`

error[E0277]: the trait bound `u64: Encode<'_,_>` is not satisfied
  --> src/db.rs:50:19
   |
50 |             .bind(property.area)
   |                   ^^^^^^^^^^^^^ the trait `Encode<'_,_>` is not implemented for `u64`

error[E0277]: the trait bound `u64: Type<_>` is not satisfied
  --> src/db.rs:50:19
   |
50 |             .bind(property.area)
   |                   ^^^^^^^^^^^^^ the trait `Type<_>` is not implemented for `u64`

error[E0277]: the trait bound `PropertyType: Encode<'_,_>` is not satisfied
  --> src/db.rs:51:19
   |
51 |             .bind(property.property_type)
   |                   ^^^^^^^^^^^^^^^^^^^^^^ the trait `Encode<'_,_>` is not implemented for `PropertyType`

error[E0277]: the trait bound `PropertyType: Type<_>` is not satisfied
  --> src/db.rs:51:19
   |
51 |             .bind(property.property_type)
   |                   ^^^^^^^^^^^^^^^^^^^^^^ the trait `Type<_>` is not implemented for `PropertyType`

error[E0277]: the trait bound `std::option::Option<u8>: Encode<'_,_>` is not satisfied
  --> src/db.rs:53:19
   |
53 |             .bind(property.num_bedrooms)
   |                   ^^^^^^^^^^^^^^^^^^^^^ the trait `Encode<'_,sqlite>>

error[E0277]: the trait bound `u8: Type<_>` is not satisfied
  --> src/db.rs:53:19
   |
53 |             .bind(property.num_bedrooms)
   |                   ^^^^^^^^^^^^^^^^^^^^^ the trait `Type<_>` is not implemented for `u8`
   |
   = note: required because of the requirements on the impl of `Type<_>` for `std::option::Option<u8>`

error[E0277]: the trait bound `std::option::Option<u8>: Encode<'_,_>` is not satisfied
  --> src/db.rs:54:19
   |
54 |             .bind(property.num_bathrooms)
   |                   ^^^^^^^^^^^^^^^^^^^^^^ the trait `Encode<'_,sqlite>>

error[E0277]: the trait bound `u8: Type<_>` is not satisfied
  --> src/db.rs:54:19
   |
54 |             .bind(property.num_bathrooms)
   |                   ^^^^^^^^^^^^^^^^^^^^^^ the trait `Type<_>` is not implemented for `u8`
   |
   = note: required because of the requirements on the impl of `Type<_>` for `std::option::Option<u8>`

error[E0277]: the trait bound `std::option::Option<u8>: Encode<'_,_>` is not satisfied
  --> src/db.rs:55:19
   |
55 |             .bind(property.num_garages)
   |                   ^^^^^^^^^^^^^^^^^^^^ the trait `Encode<'_,sqlite>>

error[E0277]: the trait bound `u8: Type<_>` is not satisfied
  --> src/db.rs:55:19
   |
55 |             .bind(property.num_garages)
   |                   ^^^^^^^^^^^^^^^^^^^^ the trait `Type<_>` is not implemented for `u8`
   |
   = note: required because of the requirements on the impl of `Type<_>` for `std::option::Option<u8>`

error[E0277]: the trait bound `&mut PoolConnection<T>: sqlx::Executor<'_>` is not satisfied
  --> src/db.rs:56:18
   |
56 |         .execute(&mut conn)
   |                  ^^^^^^^^^ the trait `sqlx::Executor<'_>` is not implemented for `&mut PoolConnection<T>`

error: aborting due to 19 prevIoUs errors

For more information about this error,try `rustc --explain E0277`.
error: Could not compile `proj`

sqlx documentation 清楚地列出了我的所有类型作为 Encode 的实现。

为什么会这样?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)