微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Rust `sqlx` 抱怨缺少 trait 实现

如何解决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 举报,一经查实,本站将立刻删除。