'rust SQLx: "expected enum `std::option::Option`" in a query but not in a similar one
I have two Rust methods selecting data with sqlx from the same SQLite table depending on two different parameters.
I cannot manage to have both working due to an expected `i64`, found enum `std::option::Option` error.
Code
// src/main.rs
use tokio;
use anyhow::Result;
use sqlx::sqlite::SqlitePool;
// The model `StorageName` that I'm retrieving is something like
pub struct StorageName {
pub _id: i64,
pub name: String,
pub url: String,
}
// This compiles only if `_id` is `Option<i64>`
async fn queryByName(pool: &SqlitePool, name: String) -> Result<Vec<StorageName>> {
let results = sqlx::query_as!(
StorageName,
"SELECT * FROM names
WHERE name = ?;",
name,
)
.fetch_all(pool)
.await?;
Ok(results)
}
// This compiles if `_id` is `i64`
// Also querying by `_id` is ok
async fn queryByURL(pool: &SqlitePool, url: String) -> Result<Vec<StorageName>> {
let results = sqlx::query_as!(
StorageName,
"SELECT * FROM names
WHERE url = ?;",
url,
)
.fetch_all(pool)
.await?;
Ok(results)
}
#[tokio::main]
async fn main() -> Result<()> {
Ok(())
}
sqllite .schema names returns
CREATE TABLE names (
_id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
url TEXT NOT NULL,
UNIQUE(name, url)
);
Cargo.toml dependencies:
[dependencies]
anyhow = "1.0.44"
sqlx = { version = "0.5", features = [ "runtime-tokio-rustls", "sqlite" ] }
tokio = { version = "1.12.0", features = ["full"] }
How can I solve this? _id cannot be both Option and i64.
[updated] I've the suspect that the plural in the table's name collides with the field someway.
Rust version is 1.56.0
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
