2

我有以下插入查询:

   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<'_, _>` 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: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<'_, _>` 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: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<'_, _>` 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: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文档清楚地列出了我的所有类型作为Encode.

为什么会这样?

4

0 回答 0