我有这个结构:
#[table_name = "clients"]
#[derive(Serialize, Deserialize, Queryable, Insertable, Identifiable, Associations)]
pub struct Client {
pub id: Option<i64>,
pub name: String,
pub rank: Option<i64>,
}
Run Code Online (Sandbox Code Playgroud)
以及以下实现:
impl Client {
pub fn get(name: String, connection: &PgConnection) -> Option<Self> {
match clients::table
.filter(clients::name.eq(&name))
.limit(1)
.load::<Client>(connection)
{
Ok(clients) => Some(clients[0]),
Err(_) => None,
}
}
}
Run Code Online (Sandbox Code Playgroud)
这给了我以下错误:
#[table_name = "clients"]
#[derive(Serialize, Deserialize, Queryable, Insertable, Identifiable, Associations)]
pub struct Client {
pub id: Option<i64>,
pub name: String,
pub rank: Option<i64>,
}
Run Code Online (Sandbox Code Playgroud)
您的错误消息说您无法将 a BigInt(a 64 bits int)查询到Option<i64>. 那是因为你忘了id在你的表声明中说它可以为空。它必须看起来像:
table! {
clients {
id -> Nullable<BigInt>,
name -> Text,
rank -> Nullable<BigInt>,
}
}
Run Code Online (Sandbox Code Playgroud)
您可以在文档中看到Queryable您正在寻找的实现。
| 归档时间: |
|
| 查看次数: |
1315 次 |
| 最近记录: |