我有这个结构:
#[table_name = "clients"]
#[derive(Serialize, Deserialize, Queryable, Insertable, Identifiable, Associations)]
pub struct Client {
pub id: Option<i64>,
pub name: String,
pub rank: Option<i64>,
}
和以下实现:
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,
}
}
}
这给了我以下错误:
.load::<Client>(connection) {
^^^^ the trait `diesel::Queryable<diesel::sql_types::BigInt, _>` is not implemented for `std::option::Option<i64>`
最佳答案
您的错误消息表明您无法查询 BigInt
(一个 64 位 int)到 Option<i64>
.那是因为你忘了说 id
在你的 table declaration 中可以为空.它必须看起来像:
table! {
clients {
id -> Nullable<BigInt>,
name -> Text,
rank -> Nullable<BigInt>,
}
}
可以看到Queryable
的实现您正在寻找in the documentation .
关于rust - 为什么 Diesel 结构中的可选字段未实现特征,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52941650/