`diesel_geography::types::GeogPoint` 没有实现特征 `serde::Deserialize<'_>`

Posted

技术标签:

【中文标题】`diesel_geography::types::GeogPoint` 没有实现特征 `serde::Deserialize<\'_>`【英文标题】:the trait `serde::Deserialize<'_>` is not implemented for `diesel_geography::types::GeogPoint``diesel_geography::types::GeogPoint` 没有实现特征 `serde::Deserialize<'_>` 【发布时间】:2019-11-03 12:17:24 【问题描述】:

我正在尝试使用 Diesel 和diesel_geography 从使用 Rust 的 PostGIS 数据库中读取数据。

这是我得到的错误:

error[E0277]: the trait bound `diesel_geography::types::GeogPoint: serde::Serialize` is not satisfied
  --> src/models.rs:11:5
   |
11 |     pub coordinates: GeogPoint,
   |     ^^^ the trait `serde::Serialize` is not implemented for `diesel_geography::types::GeogPoint`
   |
   = note: required by `serde::ser::SerializeStruct::serialize_field`

error[E0277]: the trait bound `diesel_geography::types::GeogPoint: serde::Deserialize<'_>` is not satisfied
  --> src/models.rs:11:5
   |
11 |     pub coordinates: GeogPoint,
   |     ^^^ the trait `serde::Deserialize<'_>` is not implemented for `diesel_geography::types::GeogPoint`
   |
   = note: required by `serde::de::SeqAccess::next_element`

error[E0277]: the trait bound `diesel_geography::types::GeogPoint: serde::Deserialize<'_>` is not satisfied
  --> src/models.rs:11:5
   |
11 |     pub coordinates: GeogPoint,
   |     ^^^ the trait `serde::Deserialize<'_>` is not implemented for `diesel_geography::types::GeogPoint`
   |
   = note: required by `serde::de::MapAccess::next_value`

环顾四周,我发现当有多个版本的serde用作依赖时也会发生类似的错误,这可以使用cargo tree -d进行检查。我试过了,serde 没有显示为重复的依赖项。

这是我目前的代码:

Cargo.toml

[package]
name = "123"
version = "0.1.0"
authors = ["ASD"]
edition = "2018"

[dependencies]
diesel =  version = "1.4.2", features = ["postgres"] 
serde =  version = "1.0", features = ["derive"]  
serde_json="1.0"

dotenv = "0.14.1"

diesel-geography = "0.2.0"

schema.rs

table! 
    use diesel::sql_types::*;
    use diesel_geography::sql_types::*;

    users (email) 
        email -> Varchar,
        password -> Varchar,
        coordinates -> Geography
    

models.rs

use diesel_geography::types::*;
use crate::schema::users;
use serde::Serialize, Deserialize;

#[derive(Debug, Serialize, Deserialize, Queryable, Insertable)]
#[table_name = "users"]
pub struct User 
    pub email: String,
    pub password: String,
    pub coordinates: GeogPoint

main.rs

extern crate serde;
extern crate dotenv;
#[macro_use] extern crate diesel;
//#[macro_use] extern crate serde_derive;

mod models;
mod schema;
use diesel::PgConnection;
use dotenv::dotenv;
use std::env;

use diesel::prelude::*;

fn main() 
    dotenv().ok();

    let database_url = env::var("DATABASE_URL")
        .expect("DATABASE_URL must be set");

    let connection = PgConnection::establish(&database_url)
        .expect(&format!("Error connecting to ", database_url));

    use crate::schema::users::dsl::*;
    use crate::models::User;

    let results = users
        .limit(5)
        .load::<User>(&connection)
        .expect("Error loading users");

    println!("Displaying  users", results.len());

    for user in results 
        println!("", user.email);
        println!("----------\n");
        println!("", user.password);
    

【问题讨论】:

欢迎来到 Stack Overflow!感谢您(a)寻找现有的解决方案并展示您尝试过的方法(b)提供一个产生错误的完整示例/太好了,尤其是对于您的第一个 SO 问题!下次,也请尝试最小化您的问题。您可以使用Rust-specific MRE tips 来减少您在此处发布的原始代码。谢谢! 【参考方案1】:

Serde 是optional dependency of diesel-geography。您需要启用该功能:

[dependencies]
diesel-geography =  version = "0.2.0", features = ["serde"] 

【讨论】:

以上是关于`diesel_geography::types::GeogPoint` 没有实现特征 `serde::Deserialize<'_>`的主要内容,如果未能解决你的问题,请参考以下文章