简体   繁体   中英

Rust with sqlx::postgres and i64

I'm new to Rust, and trying to implement an API using sqlx and PostgresQL. When reading about PostgresQL, it seems like it doesn't support u64 , as MySQL does. So I have changed my code to i64 , as documented here .

However it seems like some of my code still expects a u64 value:

An example of the code:

pub async fn drop_table(&self) -> Result<i64, sqlx::Error> {
    sqlx::query("DROP TABLE IF EXISTS users;")
        .execute(&*self.pool)
        .await
}

And the error it produces:

error[E0308]: mismatched types
 --> src\dao\user_dao.rs:7:9
  |
7 | /         sqlx::query("DROP TABLE IF EXISTS users;")
8 | |             .execute(&*self.pool)
9 | |             .await
  | |__________________^ expected `i64`, found `u64`
  |
  = note: expected enum `std::result::Result<i64, _>`
             found enum `std::result::Result<u64, _>`

I could probably cast the variable, but that could end up in a panic at runtime, so what would be the proper way to handle it?

I've used this as a reference application.

The reference application is using sqlx v0.3.5 and in that version of sqlx the execute method always returns a Result<u64, sqlx::Error> regardless of whether you're executing the query on a PostgresQL connection or a MySQL connection. The u64 represents the number of rows affected by executing the query and will always be unsigned. You should update the signature of drop_table to reflect that:

pub async fn drop_table(&self) -> Result<u64, sqlx::Error> {
    sqlx::query("DROP TABLE IF EXISTS users;")
        .execute(&*self.pool)
        .await
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM